SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Evangelist runeveryday's Avatar
    Join Date
    Jul 2009
    Posts
    437
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    is this function a closure in javascript?

    Code:
        function f1(){
            var n=999;
            function f2(){
                alert(n); // 999
            }
        }
    is the function f2() a closure? if not? why?

    what's and use of a closure.could you depict it in your own words. thank you

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by runeveryday View Post
    Code:
        function f1(){
            var n=999;
            function f2(){
                alert(n); // 999
            }
        }
    is the function f2() a closure? if not? why?
    It's not a closure. It's just a function declaration.

    If the f1 function returned a reference to the f2 function, that returned f2 function will then form a closure over the variables from the f1 function.

    Quote Originally Posted by runeveryday View Post
    what's and use of a closure.
    Here's the best explanation of closure that I've come across.
    How do JavaScript closures work?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Evangelist runeveryday's Avatar
    Join Date
    Jul 2009
    Posts
    437
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    your mean that if i call the f1(). then f2() will be a closure?

    a closure will form if a function B in a function A. and function A is called. then function B is a closure. am i right? thank you.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by runeveryday View Post
    your mean that if i call the f1(). then f2() will be a closure?
    No, it won't. It would only be a closure if the f1 function returned a reference to the f2 function.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,813
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by runeveryday View Post
    your mean that if i call the f1(). then f2() will be a closure?
    No. The closure would be on the variable "n" but only if the f1 function returned the f2 functon so that f2 can be called after f1 has already finished running (and where the "n" variable would have already gone out of scope except for the fact that there is still a way to reference it via f2.

    The following would be a closure:

    Code:
        function f1(){
            var n=999;
            function f2(){
                alert(n); // 999
            }
            return f2;
        }
    
    a = f1();
    
    a();
    With that code the last line a() will run the alert to display the 999 even though the call to f1 completed and n would have gone out of scope except for the closure.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  6. #6
    SitePoint Evangelist runeveryday's Avatar
    Join Date
    Jul 2009
    Posts
    437
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    felgall, your mean a closure will be:fuction B in function A. and in function A. there must be fuction B. then to function A's variable. fuction B is a closure. am i right?

  7. #7
    SitePoint Evangelist runeveryday's Avatar
    Join Date
    Jul 2009
    Posts
    437
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    felgall. to sum it up. according to your answer. the benefit of the closure is if outside the function. i can use the closure(f2) to reference the variable(on the above is n). am i right?thank you.

  8. #8
    SitePoint Evangelist runeveryday's Avatar
    Join Date
    Jul 2009
    Posts
    437
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    anyone knows? am i right?

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by runeveryday View Post
    anyone knows? am i right?
    That seems to be right.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •