On 12/16/10, Darth Vincent Binamira <[email protected]> wrote:
>> Next, in evaluation of statements, the IfStatement is evaluated. So
>> what is `"a" in window"`? result? In a web browser, the global object
>> its he global variable object is the window. Thus `"a" in window` is
>> true.
>>
>> So `"a" in window` is true, and the Logical NOT operator (!) converts
>> that to false. And:
>> | if( !("a" in window) )
>>
>> Must be false!
>
> I just tested this out in firebug's console and I'm a little bit confused.
> `"a" in window` returned false, and `!("a" in window)` returned true.
>
> Can you give a bit of clarification?
>
I assume Firebug uses eval and IIRC it uses `with` for augmented
scope. Expect different results in Firebug.

> Sorry for asking this, but its really confusing. Thanks.
>
I've added a bit to my explanation.

[...]

>> Example:
>> |  if (!("a" in window)) {
>> |    var a = 1;
>> |  }
>> |  alert(a);
>>
>> There are two passes of the code here. First, there is Variable
>> Instantiation. This is where identifiers with variable statement,
>> FunctionDeclaration, and parameters (for function code) are added to
>> the variable object (VO), and given attribute {DontDelete}.

So in Variable Instantiation, the code is first scanned for variable
declarations (among other things).
http://bclary.com/2004/11/07/#a-10.1.3

The assigment `a = 1` is not part of the declaration, so it is skipped.

When the interpreter sees `var a`, if the VO does not have an `a`
property, then `a` is added as a property of the VO and given the
value `undefined`.

The global VO is also the global object, so the global object now has
an `a` property. The global object is aliased by `window`. Thus, after
variable instantiation, `"a" in window` is true.

[...]

>>
>> The second pass is evaluation of Statements, such as the IfStatement
>> or the assigmentExpression `a = 1`.
>>
>> In Variable Instantiation, `a` is added to the VO, if it does not
>> already exist, and given value `undefined`. If the VO already has an
>> `a` property, its value is unchanged.
>>
>> Next, in evaluation of statements, the IfStatement is evaluated. So
>> what is `"a" in window"`? result? In a web browser, the global object
>> its he global variable object is the window. Thus `"a" in window` is
>> true.
>>
>> So `"a" in window` is true, and the Logical NOT operator (!) converts
>> that to false. And:
>> | if( !("a" in window) )
>>
>> Must be false!
>>
>> The result of the program is the creation of a global identifier `a`
>> with value `undefined` and attributes {DontDelete}.

[...]

I should note that in ECMA-262 Ed. 5, the "variable object" was
renamed to "lexical environment record".
http://ecma262-5.com/ELS5_Section_10.htm#Section_10.5

As stated, in variable instantiation, the variable declarations are
added to the VO. I did not explain that FunctionDeclarations and
formal parameters are added to the VO. They are, as explained in the
spec.
See also: 
https://groups.google.com/group/comp.lang.javascript/browse_thread/thread/26a9efc584bff4e4/7be4bbd67360c70f?hl=en&;

- which links to the closures article which explains in more detail.
Of particular relevance to this discussion:
http://jibbering.com/faq/notes/closures/#clIRExSc
-- 
Garrett

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to