closure question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JavascriptProgrammer

    closure question

    In the following code:
    -----------------------
    function get() {
    return function() {
    alert(x);
    }
    };
    function foo(s) {
    var x = s;
    this.getX = get();
    }
    var f = new foo("hello");
    f.getX()
    --------------------------

    Instead of printing "hello", f.getX() gives a JS error
    of ("x is not defined").

    Can someone explain Why in detail ?

  • RobG

    #2
    Re: closure question

    On Sep 24, 2:45 pm, JavascriptProgr ammer <nowh...@nowher e.comwrote:
    In the following code:
    -----------------------
    function get() {
    return function() {
            alert(x);
    The x here is not declared, so when the function is called it will go
    looking for it on its scope chain.
            }};
    >
    function foo(s) {
            var x = s;
    x here is a local variable of the function foo.
            this.getX = get();
            }
    var f = new foo("hello");
    f doesn't have an x property, it was declared as a property of the
    constructor. The function get() was called from inside the
    constructor, but since it's declared outside the constructor, it
    doesn't have a closure to x.

    f.getX()
    --------------------------
    >
    Instead of printing "hello", f.getX() gives a JS error
    of ("x is not defined").
    'cos it ain't. :-)

    Move the function declaration for get inside the constructor, or use a
    statement:


    function foo(s) {

    function get() {
    return function() {
    alert(x);
    }
    }

    var x = s;
    this.getX = get();
    }


    or, for simplicity's sake:

    function foo(s) {
    this.getX = function() {
    alert(s);
    }
    }
    Can someone explain Why in detail ?
    There is no closure. Closures are created by how you declare a
    function, not by how you call it. To create a closure, you declare a
    function (or use a function expression) inside another function:

    function foo() {
    var x;
    function bar() { // Create a closure
    alert(x); // Use it to access foo's x
    }
    }


    --
    Rob

    Comment

    • Conrad Lender

      #3
      Re: closure question

      On 2008-09-24 06:45, JavascriptProgr ammer wrote:
      Instead of printing "hello", f.getX() gives a JS error
      of ("x is not defined").
      >
      Can someone explain Why in detail ?
      What you expected would indeed be correct in a dynamically scoped
      language, but JavaScript uses lexical scope. Here's a good explanation
      of the difference between the two:

      http://en.wikipedia.org/wiki/Scope_(programming)


      - Conrad

      Comment

      • Joost Diepenmaat

        #4
        Re: closure question

        Conrad Lender <crlender@yahoo .comwrites:
        On 2008-09-24 06:45, JavascriptProgr ammer wrote:
        >Instead of printing "hello", f.getX() gives a JS error
        >of ("x is not defined").
        >>
        >Can someone explain Why in detail ?
        >
        What you expected would indeed be correct in a dynamically scoped
        language, but JavaScript uses lexical scope. Here's a good explanation
        of the difference between the two:
        >
        http://en.wikipedia.org/wiki/Scope_(programming)
        Correct. And to expand a bit, as far as I know closures are /defined/ to
        work on lexicals. You can do similar things with dynamic variables (if
        your language supports them) but many of the really interesting uses of
        anonymous functions require lexical scope and closures:


        function make_closure() {
        var i = 0;
        return function () { return ++i };
        }

        var c1 = make_closure();
        c1(); // 1

        var c2 = make_closure();
        c2(); // 1

        c1(); // 2
        c1(); // etc...
        c2();
        c1();

        See also:
        http://en.wikipedia.org/wiki/Closure_(computer_science)

        --
        Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: closure question

          RobG wrote:
          JavascriptProgr ammer wrote:
          >function foo(s) {
          > var x = s;
          >
          x here is a local variable of the function foo.
          >
          > this.getX = get();
          > }
          >var f = new foo("hello");
          >
          f doesn't have an x property, it was declared as a property of the
          constructor. [...]
          Because `x' was declared a local variable of the constructor's *execution
          context*, it became a property of its *Variable Object*. As became `s', BTW.


          PointedEars
          --
          Prototype.js was written by people who don't know javascript for people
          who don't know javascript. People who don't know javascript are not
          the best source of advice on designing systems that use javascript.
          -- Richard Cornford, cljs, <f806at$ail$1$8 300dec7@news.de mon.co.uk>

          Comment

          Working...