Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Unexpected scope question

Reply
Thread Tools

Unexpected scope question

 
 
risingfish
Guest
Posts: n/a
 
      02-06-2012
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
 
Reply With Quote
 
 
 
 
Michael Haufe (TNO)
Guest
Posts: n/a
 
      02-06-2012
On Feb 5, 9:39*pm, risingfish <(E-Mail Removed)> 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?
 
Reply With Quote
 
 
 
 
Scott Sauyet
Guest
Posts: n/a
 
      02-06-2012
On Feb 5, 10:39*pm, risingfish <(E-Mail Removed)> 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/pu...T/Ecma-262.pdf
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Lexical scope vs. dynamic scope Xah Lee Java 0 02-26-2009 10:08 AM
Unexpected scope range Andreas Warberg Ruby 9 12-06-2006 07:50 PM
Scope - do I need two identical classes, each with different scope? ann Java 13 09-13-2005 03:07 AM
How do namespace scope and class scope differ? Steven T. Hatton C++ 9 07-19-2005 06:07 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM



Advertisments