[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Is this Simple Example of closure correct?

Posted on 2012-08-17
5
Medium Priority
?
333 Views
Last Modified: 2012-08-20
Hello Experts,

I am trying to understand closures in javascript and have written a very simple Closure Example.
Here is the link to the jsFiddle.Please let me know if the code is written correct.I am getting the correct output though.

http://jsfiddle.net/mRuBt/1/

$(function(){
  //1.Create a function
  function sayHello(name){
    //Create a variable
    var text  = "Hello "+name;
    //Now create an inner function
    var sayAlert = $("#click").click(function(){
      alert(text);
    });
    return sayAlert;
  }
  sayHello('Closure Example !');
});

Open in new window

0
Comment
Question by:dvcphp
  • 2
4 Comments
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38307667
Essentially yes - it demonstrates a closure but to fully understand what it is you might want to expand on what it is that is going on.

for instance that a contained function within another function has access to the enclosing functions variables.

Here is a more comprehensive definition.

http://jibbering.com/faq/notes/closures/

But essentially you have the essence of it.
0
 

Author Comment

by:dvcphp
ID: 38308613
Thank you JulianH.

Could you please explain why we have () in the end of this closure example

function say(name) {
  var text = name; // variable
  return function() { alert(text); }
}
say('Hello')();

The () in the end is confusing and if O remove and run the program,it doesn't work :)
Thanks in advance..
0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 280 total points
ID: 38308870
0
 
LVL 60

Accepted Solution

by:
Julian Hansen earned 1720 total points
ID: 38308952
Because the say() function is returning a function and you want to invoke that function when it returns

It is a shorthand way of doing this

function say(name) {
  var text = name; // variable
  return function() { alert(text); }
}
// get a reference to the function
var fn = say('Hello');
// call the function
fn();

Open in new window

Shorthand
say('Hello')(); // Get the reference and call it in one statement

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month17 days, 14 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question