Unexpected scope question

Discussion in 'Javascript' started by risingfish, Feb 6, 2012.

  1. risingfish

    risingfish Guest

    Hello, I would call myself a fairly competent JavaScript programmer,
    but I ran into something expected today. Here's a snippet:

    NS.MyClass = function () {

    var initialize = function() {
    /* do something */
    NS.Instances[<uniqueId>] = this;
    }

    initialize();
    }

    var obj = new NS.MyClass();

    The object that ends up in NS.Instances[<uniqueId>] is DOMWindow, not
    the particular instance of that class. I can obviously fix it with
    call() but I've found a hole in my knowledge and am trying to
    understand. Could someone explain what is happening there?

    Thanks!
    R
    risingfish, Feb 6, 2012
    #1
    1. Advertising

  2. On Feb 5, 9:39 pm, risingfish <> wrote:
    > Hello, I would call myself a fairly competent JavaScript programmer,
    > but I ran into something expected today. Here's a snippet:
    >
    > NS.MyClass = function () {
    >
    >     var initialize = function() {
    >         /* do something */
    >         NS.Instances[<uniqueId>] = this;
    >     }
    >
    >     initialize();
    >
    > }
    >
    > var obj = new NS.MyClass();
    >
    > The object that ends up in NS.Instances[<uniqueId>] is DOMWindow, not
    > the particular instance of that class. I can obviously fix it with
    > call() but I've found a hole in my knowledge and am trying to
    > understand. Could someone explain what is happening there?



    The bigger question is why you've decided to write JavaScript in this
    manner. Why do you have an inner function in the first place?
    Michael Haufe (TNO), Feb 6, 2012
    #2
    1. Advertising

  3. risingfish

    Scott Sauyet Guest

    On Feb 5, 10:39 pm, risingfish <> wrote:
    > Hello, I would call myself a fairly competent JavaScript programmer,
    > but I ran into something expected today. Here's a snippet:
    >
    > NS.MyClass = function () {
    >
    >     var initialize = function() {
    >         /* do something */
    >         NS.Instances[<uniqueId>] = this;
    >     }
    >
    >     initialize();
    >
    > }
    >
    > var obj = new NS.MyClass();
    >
    > The object that ends up in NS.Instances[<uniqueId>] is DOMWindow, not
    > the particular instance of that class. I can obviously fix it with
    > call() but I've found a hole in my knowledge and am trying to
    > understand.


    From version 5.1 of the ECMA-262 spec (June, 2011) [1]:

    | 10.4.3 Entering Function Code
    | The following steps are performed when control enters the execution
    | context for function code contained in function object F, a caller
    | provided thisArg, and a caller provided argumentsList:
    |
    | 1. If the function code is strict code, set the ThisBinding to
    thisArg.
    | 2. Else if thisArg is null or undefined, set the ThisBinding to the
    | global object.
    | 3. Else if Type(thisArg) is not Object, set the ThisBinding to
    | ToObject(thisArg).
    | 4. Else set the ThisBinding to thisArg.
    | ...

    So, when you call `initialize` from within your constructor function
    without specifically binding `this`, the ThisBinding defaults to the
    global object.

    As you say, you can fix this with `call`:

    | initialize.call(this);

    You also might want to reconsider having the initialize function
    redefined for each call to the constructor by storing it in a local
    closure. Or, if it's as simple as what you've exposed here, remove
    the inner function altogether, as Michael suggests.

    -- Scott

    [1]: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
    Scott Sauyet, Feb 6, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Opal
    Replies:
    12
    Views:
    925
    Paul Opal
    Oct 11, 2004
  2. ann
    Replies:
    13
    Views:
    646
    Patricia Shanahan
    Sep 13, 2005
  3. Steven T. Hatton
    Replies:
    9
    Views:
    457
  4. Xah Lee
    Replies:
    0
    Views:
    2,221
    Xah Lee
    Feb 26, 2009
  5. Andreas Warberg

    Unexpected scope range

    Andreas Warberg, Dec 5, 2006, in forum: Ruby
    Replies:
    9
    Views:
    128
    Eric Hodel
    Dec 6, 2006
Loading...

Share This Page