Is this Simple Example of closure correct?

Posted on 2012-08-17
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.

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

Open in new window

Question by:dvcphp
    LVL 50

    Expert Comment

    by:Julian Hansen
    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.

    But essentially you have the essence of it.

    Author Comment

    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); }

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

    Assisted Solution

    by:Michel Plungjan
    LVL 50

    Accepted Solution

    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

    Open in new window

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

    Open in new window


    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
    How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
    The viewer will learn how to dynamically set the form action using jQuery.
    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…

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now