<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>What is the relationship between global object enumerators, execution contexts, activation objects, variable objects and this?</title><link>http://blogs.msdn.com/ericlippert/archive/2005/05/04/what-is-the-relationship-between-global-object-enumerators-execution-contexts-activation-objects-variable-objects-and-this.aspx</link><description>UPDATE: I am WRONG WRONG WRONG . Brendan Eich, the original creator of JavaScript, was kind enough to point out at great length that I was WRONG WRONG WRONG in my conclusions earlier today. Somehow I managed to miss the key section in the spec -- my search-fu</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: What is the relationship between global object enumerators, execution contexts, activation objects, variable objects and this?</title><link>http://blogs.msdn.com/ericlippert/archive/2005/05/04/what-is-the-relationship-between-global-object-enumerators-execution-contexts-activation-objects-variable-objects-and-this.aspx#414784</link><pubDate>Thu, 05 May 2005 02:01:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:414784</guid><dc:creator>brendan@mozilla.org</dc:creator><description>Sorry, but the analysis in this blog entry is just wrong.  First, the &amp;quot;current variable object&amp;quot; that is referenced here:&lt;br&gt;&lt;br&gt;2.      Create a property of the current variable object […] with name Identifier and value Result(1).&lt;br&gt;&lt;br&gt;(ECMA-262 Edition 3, section 13, second step for first production) is the variable object for the execution content in which the FunctionDeclaration is being executed.  It's not the variable object associated with activations of that function.&lt;br&gt;&lt;br&gt;Second, |this| binding is completely specified by ECMA-262 for all function syntax.  Follow these parts of the spec (my comments in [brackets]):&lt;br&gt;&lt;br&gt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;spec citation starts here&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&lt;br&gt;&lt;br&gt;10.2.1 Global Code&lt;br&gt;&lt;br&gt;* The scope chain is created and initialised to contain the global object and no others.&lt;br&gt;* Variable instantiation is performed using the global object as the variable object and using property attributes { DontDelete }.&lt;br&gt;* The this value is the global object.&lt;br&gt;&lt;br&gt;[Right here, the claim in this blog entry that &amp;quot;There is no requirement that [function foo(){} and this.foo = function(){}] be the same&amp;quot; can be seen to be false for function declarations and statements in global code.&lt;br&gt;&lt;br&gt;From the second bulleted item, it's also clear that JScript has a bug if it instantiates declared function identifiers as variables with the DontEnum attribute. Declared functions result in enumerable properties of their declarations' variable object in ECMA-262.]&lt;br&gt;&lt;br&gt;10.2.3 Function Code&lt;br&gt;. . .&lt;br&gt;* The caller provides the this value. If the this value provided by the caller is not an object (including the case where it is null), then the this value is the global object.&lt;br&gt;&lt;br&gt;[the caller provides the this value, and any non-object this value is replaced with a reference to the global object.]&lt;br&gt;&lt;br&gt;11.2.3 Function Calls&lt;br&gt;&lt;br&gt;The production CallExpression : MemberExpression Arguments is evaluated as follows:&lt;br&gt;&lt;br&gt;1. Evaluate MemberExpression.&lt;br&gt;2. Evaluate Arguments, producing an internal list of argument values (section 11.2.4).&lt;br&gt;3. Call GetValue(Result(1)).&lt;br&gt;4. If Type(Result(3)) is not Object, throw a TypeError exception.&lt;br&gt;5. If Result(3) does not implement the internal [[Call]] method, throw a TypeError exception.&lt;br&gt;6. If Type(Result(1)) is Reference, Result(6) is GetBase(Result(1)). Otherwise, Result(6) is null.&lt;br&gt;7. If Result(6) is an activation object, Result(7) is null. Otherwise, Result(7) is the same as Result(6). 8. Call the [[Call]] method on Result(3), providing Result(7) as the this value and providing the list Result(2) as the argument values.&lt;br&gt;9. Return Result(8).&lt;br&gt;&lt;br&gt;[Step 8 binds this to the base object of the reference by which the function is denoted in the call expression, censoring activation objects in Step 7. Gecko's JS implementation follows this step.&lt;br&gt;&lt;br&gt;Just to finish off any doubt about this binding of a function called by its unqualified name -- foo() instead of window.foo() or this.foo() at top-leve:]&lt;br&gt;&lt;br&gt;10.1.4 Scope Chain and Identifier Resolution&lt;br&gt;&lt;br&gt;Every execution context has associated with it a scope chain. A scope chain is a list of objects that are searched when evaluating an Identifier. When control enters an execution context, a scope chain is created and populated with an initial set of objects, depending on the type of code. During execution within an execution context, the scope chain of the execution context is affected only by with statements (section 12.10) and catch clauses (section 12.14).&lt;br&gt;&lt;br&gt;During execution, the syntactic production PrimaryExpression : Identifier is evaluated using the following algorithm:&lt;br&gt;&lt;br&gt;1. Get the next object in the scope chain. If there isn't one, go to step 5.&lt;br&gt;2. Call the [[HasProperty]] method of Result(1), passing the Identifier as the property.&lt;br&gt;3. If Result(2) is true, return a value of type Reference whose base object is Result(1) and whose property name is the Identifier.&lt;br&gt;4. Go to step 1.&lt;br&gt;5. Return a value of type Reference whose base object is null and whose property name is the Identifier.&lt;br&gt;&lt;br&gt;The result of evaluating an identifier is always a value of type Reference with its member name component equal to the identifier string.&lt;br&gt;&lt;br&gt;[In short, global code of the form foo() calling a declared function foo whose declaration was processed correctly according to the spec will bind this to the global object, which was the variable object at the time the function declaration was processed.]&lt;br&gt;&lt;br&gt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;spec citation ends here&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&lt;br&gt;&lt;br&gt;I believe my ECMA TG1 colleague from MS, Rok Yu, knows the ECMA-262 spec pretty well; you might want to talk to him.&lt;br&gt;&lt;br&gt;/be</description></item><item><title>re: What is the relationship between global object enumerators, execution contexts, activation objects, variable objects and this?</title><link>http://blogs.msdn.com/ericlippert/archive/2005/05/04/what-is-the-relationship-between-global-object-enumerators-execution-contexts-activation-objects-variable-objects-and-this.aspx#414797</link><pubDate>Thu, 05 May 2005 02:52:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:414797</guid><dc:creator>Dean Edwards</dc:creator><description>This code shows that it is a bug in JScript:&lt;br&gt;&lt;br&gt;&lt;br&gt;function test(){};&lt;br&gt;   for (var i in window) if (i == &amp;quot;test&amp;quot;) alert(i);&lt;br&gt;   alert(&amp;quot;test&amp;quot; in window);&lt;br&gt;&lt;br&gt;&lt;br&gt;You can't enumerate &amp;quot;test&amp;quot; but it is a member of the the global object. You can change &amp;quot;this&amp;quot; for &amp;quot;window&amp;quot; and you still get the same bug.</description></item><item><title>re: What is the relationship between global object enumerators, execution contexts, activation objects, variable objects and this?</title><link>http://blogs.msdn.com/ericlippert/archive/2005/05/04/what-is-the-relationship-between-global-object-enumerators-execution-contexts-activation-objects-variable-objects-and-this.aspx#414816</link><pubDate>Thu, 05 May 2005 05:10:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:414816</guid><dc:creator>Eric Lippert</dc:creator><description>Good heavens.&lt;br&gt;&lt;br&gt;Thanks Brendan, you are totally correct. I don't know how I missed that when I read over the spec.&lt;br&gt;&lt;br&gt;I'll rewrite this tomorrow.</description></item><item><title>re: What is the relationship between global object enumerators, execution contexts, activation objects, variable objects and this?</title><link>http://blogs.msdn.com/ericlippert/archive/2005/05/04/what-is-the-relationship-between-global-object-enumerators-execution-contexts-activation-objects-variable-objects-and-this.aspx#414842</link><pubDate>Thu, 05 May 2005 07:22:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:414842</guid><dc:creator>brendan@mozilla.org</dc:creator><description>I wrote:&lt;br&gt;&lt;br&gt;Right here, the claim in this blog entry that &amp;quot;There is no requirement that [function foo(){} and this.foo = function(){}] be the same&amp;quot; can be seen to be false for function declarations and statements in global code.&lt;br&gt;&lt;br&gt;but of course there's one difference, which does not have anything to do with which object binds to this, or in which object foo is bound: in the declared function case, the function identifier is instantiated as a variable with attributes {DontDelete}.  Simply assigning to this.foo sets no such attribute.&lt;br&gt;&lt;br&gt;An obscure case in ECMA-262: variable instantiation in eval code does not use DontDelete.  I don't recall the rationale, but it makes a kind of common sense: you can't delete declared vars unless they're computed by eval (in which case they might be conditional, and should be deletable).&lt;br&gt;&lt;br&gt;/be</description></item><item><title>JavaScript closures</title><link>http://blogs.msdn.com/ericlippert/archive/2005/05/04/what-is-the-relationship-between-global-object-enumerators-execution-contexts-activation-objects-variable-objects-and-this.aspx#464042</link><pubDate>Mon, 12 Sep 2005 19:32:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:464042</guid><dc:creator>Worm in liquid maze</dc:creator><description>I've recently stumbled across this (&lt;a rel="nofollow" target="_new" href="http://www.jibbering.com/faq/faq_notes/closures.html"&gt;http://www.jibbering.com/faq/faq_notes/closures.html&lt;/a&gt;) article. It...</description></item><item><title>re: What is the relationship between global object enumerators, execution contexts, activation objects, variable objects and this?</title><link>http://blogs.msdn.com/ericlippert/archive/2005/05/04/what-is-the-relationship-between-global-object-enumerators-execution-contexts-activation-objects-variable-objects-and-this.aspx#851109</link><pubDate>Sat, 21 Oct 2006 00:54:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:851109</guid><dc:creator>Lev Serebryakov</dc:creator><description>&lt;p&gt;Is here any workaround? My JS library want to register all user's objects with names, matched by pattern, on start-up. In Gecjo-based browsers enumeration of global object works. In IE here is a bug. But, may be, here is some woraround?&lt;/p&gt;
</description></item><item><title>re: What is the relationship between global object enumerators, execution contexts, activation objects, variable objects and this?</title><link>http://blogs.msdn.com/ericlippert/archive/2005/05/04/what-is-the-relationship-between-global-object-enumerators-execution-contexts-activation-objects-variable-objects-and-this.aspx#7936274</link><pubDate>Thu, 28 Feb 2008 19:32:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7936274</guid><dc:creator>Jörg Schaible</dc:creator><description>&lt;p&gt;At least I can confirm that it is a pure IE thingy. Run the JavaScript with cscript from command line and it is possible to enumerate the functions of the global object.&lt;/p&gt;
</description></item><item><title>re: What is the relationship between global object enumerators, execution contexts, activation objects, variable objects and this?</title><link>http://blogs.msdn.com/ericlippert/archive/2005/05/04/what-is-the-relationship-between-global-object-enumerators-execution-contexts-activation-objects-variable-objects-and-this.aspx#8651548</link><pubDate>Wed, 25 Jun 2008 10:31:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8651548</guid><dc:creator>Andrew Csontos</dc:creator><description>&lt;p&gt;Erik,&lt;/p&gt;
&lt;p&gt;Like J&amp;#246;rg, I tried your script to enumerate the properties of the __global__ object from cscript and it worked. &lt;/p&gt;
&lt;p&gt;However from ASP (classic) it does not appear that you can enumerate the properties of the global &amp;quot;this&amp;quot; object.&lt;/p&gt;
&lt;p&gt;Is that a limitation of the ASP JScript host?&lt;/p&gt;
&lt;p&gt;After poking around for a few hours, i couldn't find any answers.&lt;/p&gt;
</description></item><item><title>re: What is the relationship between global object enumerators, execution contexts, activation objects, variable objects and this?</title><link>http://blogs.msdn.com/ericlippert/archive/2005/05/04/what-is-the-relationship-between-global-object-enumerators-execution-contexts-activation-objects-variable-objects-and-this.aspx#8651557</link><pubDate>Wed, 25 Jun 2008 10:35:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8651557</guid><dc:creator>Andrew Csontos</dc:creator><description>&lt;p&gt;The error is Microsoft JScript runtime error: Object doesn't support this property or method and it is thrown on the line:&lt;/p&gt;
&lt;p&gt;__global__.visibleToIE = function() {&lt;/p&gt;
&lt;p&gt;	Response.Write(&amp;quot;IE sees me&amp;quot;);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;So it appears you cannot set properties on the global &amp;quot;this&amp;quot; object in ASP JScript either.&lt;/p&gt;
</description></item></channel></rss>