<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: ECMA-262-3 in detail. Chapter 3. This.</title>
	<atom:link href="http://dmitrysoshnikov.com/ecmascript/chapter-3-this/feed/" rel="self" type="application/rss+xml" />
	<link>http://dmitrysoshnikov.com/ecmascript/chapter-3-this/</link>
	<description>by Dmitry Soshnikov</description>
	<lastBuildDate>Sun, 13 May 2012 08:41:14 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<item>
		<title>By: Dmitry Soshnikov</title>
		<link>http://dmitrysoshnikov.com/ecmascript/chapter-3-this/comment-page-1/#comment-14057</link>
		<dc:creator>Dmitry Soshnikov</dc:creator>
		<pubDate>Wed, 09 May 2012 22:53:16 +0000</pubDate>
		<guid isPermaLink="false">http://dmitrysoshnikov.com/?p=415#comment-14057</guid>
		<description>@&lt;b&gt;Shawn&lt;/b&gt;

I see correct output in your example. I guess output format of the consoles may confuse, but:

&lt;code&gt;foo.bar();&lt;/code&gt; correctly matches to &lt;code&gt;Object { bar=function()}&lt;/code&gt; -- this is the &lt;code&gt;foo&lt;/code&gt; object.

&lt;code&gt;test()&lt;/code&gt; maps to the &lt;code&gt;Window&lt;/code&gt; which is the global object in the browser environment.

&lt;code&gt;fooTwo===fooTwo.prototype.constructor: true&lt;/code&gt; is really true.

&lt;code&gt;fooTwo()&lt;/code&gt; also correctly shows &lt;code&gt;Window&lt;/code&gt; (global).

&lt;code&gt;fooTwo {}&lt;/code&gt; maps to &lt;code&gt;fooTwo.prototype&lt;/code&gt;.</description>
		<content:encoded><![CDATA[<p>@<b>Shawn</b></p>
<p>I see correct output in your example. I guess output format of the consoles may confuse, but:</p>
<p><code>foo.bar();</code> correctly matches to <code>Object { bar=function()}</code> &#8212; this is the <code>foo</code> object.</p>
<p><code>test()</code> maps to the <code>Window</code> which is the global object in the browser environment.</p>
<p><code>fooTwo===fooTwo.prototype.constructor: true</code> is really true.</p>
<p><code>fooTwo()</code> also correctly shows <code>Window</code> (global).</p>
<p><code>fooTwo {}</code> maps to <code>fooTwo.prototype</code>.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shawn</title>
		<link>http://dmitrysoshnikov.com/ecmascript/chapter-3-this/comment-page-1/#comment-14008</link>
		<dc:creator>Shawn</dc:creator>
		<pubDate>Tue, 08 May 2012 10:53:54 +0000</pubDate>
		<guid isPermaLink="false">http://dmitrysoshnikov.com/?p=415#comment-14008</guid>
		<description>Dmitry，I have test the code in the firefox and chrome.but some thing is not appear as you said.
 [js] var foo={
            bar:function(){
                return this;
            }
        };
        console.log(foo.bar());//foo
        //Again we have the value of type Reference which base is foo object and which
        //is foo object and which is used as this value at bar function activation
            //var fooBarReference={
            //base:foo,
            //propertyName:&#039;bar&#039;
            //};
        
        //However,activating the same function with another form of a call expression,
        //we have already other this value
        var test=foo.bar;
        console.log(test());//global
        //Because test,being the identifier,produces other value of Reference type,which
        //base(the global object) is used as this value;
            //var testReference={
            //  base:global,
            //  propertyName:&#039;test&#039;
            //}
            
        function fooTwo(){
            return this;
        }
        console.log(&quot;fooTwo===fooTwo.prototype.constructor:&quot;,fooTwo===fooTwo.prototype.constructor);
console.log(fooTwo());
console.log(fooTwo.prototype.constructor()); [/js]

the resluts is :
[js]Object { bar=function()}
Window 
fooTwo===fooTwo.prototype.constructor: true
Window 
fooTwo {}[/js]

could you give me some suggests about this question.thank u</description>
		<content:encoded><![CDATA[<p>Dmitry，I have test the code in the firefox and chrome.but some thing is not appear as you said.</p>
<pre class="brush: jscript; title: ;"> var foo={
            bar:function(){
                return this;
            }
        };
        console.log(foo.bar());//foo
        //Again we have the value of type Reference which base is foo object and which
        //is foo object and which is used as this value at bar function activation
            //var fooBarReference={
            //base:foo,
            //propertyName:'bar'
            //};

        //However,activating the same function with another form of a call expression,
        //we have already other this value
        var test=foo.bar;
        console.log(test());//global
        //Because test,being the identifier,produces other value of Reference type,which
        //base(the global object) is used as this value;
            //var testReference={
            //  base:global,
            //  propertyName:'test'
            //}

        function fooTwo(){
            return this;
        }
        console.log(&quot;fooTwo===fooTwo.prototype.constructor:&quot;,fooTwo===fooTwo.prototype.constructor);
console.log(fooTwo());
console.log(fooTwo.prototype.constructor()); </pre>
<p>the resluts is :</p>
<pre class="brush: jscript; title: ;">Object { bar=function()}
Window
fooTwo===fooTwo.prototype.constructor: true
Window
fooTwo {}</pre>
<p>could you give me some suggests about this question.thank u</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: james li</title>
		<link>http://dmitrysoshnikov.com/ecmascript/chapter-3-this/comment-page-1/#comment-13128</link>
		<dc:creator>james li</dc:creator>
		<pubDate>Thu, 29 Mar 2012 09:18:51 +0000</pubDate>
		<guid isPermaLink="false">http://dmitrysoshnikov.com/?p=415#comment-13128</guid>
		<description>great article! Thanks, learned a lot.</description>
		<content:encoded><![CDATA[<p>great article! Thanks, learned a lot.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Luiz</title>
		<link>http://dmitrysoshnikov.com/ecmascript/chapter-3-this/comment-page-1/#comment-6051</link>
		<dc:creator>Luiz</dc:creator>
		<pubDate>Sat, 05 Mar 2011 13:40:06 +0000</pubDate>
		<guid isPermaLink="false">http://dmitrysoshnikov.com/?p=415#comment-6051</guid>
		<description>Dmitry, 
thanks a lot for taking time to explain the details. Those are pretty subtle issues that are not discussed anywhere except your articles.</description>
		<content:encoded><![CDATA[<p>Dmitry,<br />
thanks a lot for taking time to explain the details. Those are pretty subtle issues that are not discussed anywhere except your articles.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dmitry A. Soshnikov</title>
		<link>http://dmitrysoshnikov.com/ecmascript/chapter-3-this/comment-page-1/#comment-5997</link>
		<dc:creator>Dmitry A. Soshnikov</dc:creator>
		<pubDate>Wed, 02 Mar 2011 17:57:46 +0000</pubDate>
		<guid isPermaLink="false">http://dmitrysoshnikov.com/?p=415#comment-5997</guid>
		<description>@&lt;b&gt;Luiz&lt;/b&gt;

&lt;blockquote&gt;1) In function &lt;code&gt;getValue()&lt;/code&gt; I had to use return v.base instead of &lt;code&gt;v.base[v.propertyName]&lt;/code&gt;;&lt;/blockquote&gt;

No, it&#039;s not correct, should be nevertheless &lt;code&gt;v.base[v.propertyName]&lt;/code&gt;.

The mistake you made is that you used not correct &lt;code&gt;base&lt;/code&gt; component for e.g. &lt;code&gt;x&lt;/code&gt; reference. You used &lt;code&gt;x&lt;/code&gt; as the base, but it should be the global object. Once again, the base component of the reference is an object to which the property belongs. Global variable &lt;code&gt;x&lt;/code&gt; belongs to global object, therefore global object is its base.

You have (wrong):

[js]var x = 10;

// wrong 
var xReference = new Reference(x,&quot;x&quot;);[/js]

and it should be:

[js]// global object -- we need it to
// use as a base of global variables!

var global = this;

var x = 10;
 
// base is &quot;global&quot; (not &quot;x&quot; as in your example),
// since variable &quot;x&quot; is a global variable

var xReference = new Reference(global, &quot;x&quot;);[/js]

Also if you defined your own &lt;code&gt;ReferenceError&lt;/code&gt; constructor (though, it&#039;s a native built-in function), then for tests you may not throw it in &lt;code&gt;getValue&lt;/code&gt;, but just return. So the complete code to test may look like this:

[js]// global object
var global = this;

// global variable &quot;x&quot;
var x = 10;

// reference for global variable &quot;x&quot;:
// base: global, propertyName: &quot;x&quot;
var xReference = new Reference(global, &quot;x&quot;);

console.log(&quot;Type of &#039;x&#039;: &quot;, typeof x); // &quot;number&quot;
console.log(&quot;Value of &#039;x&#039;: &quot;, getValue(xReference)); // 10

// unresolved reference for non-existing
// variable &quot;y&quot; (i.e. base is null)

var yReference = new Reference(null, &quot;y&quot;);

console.log(&quot;Type of &#039;y&#039;: &quot;, typeof y ); // &quot;undefined&quot;
console.log(&quot;Value of &#039;y&#039;: &quot;, getValue(yReference)); // ReferenceError

function Reference(base, propertyName) {
  this.base = base;
  this.propertyName = propertyName;
}

function ReferenceError(error) {
    return &quot;Reference Error: &quot; + error;
}

function getValue(v) {
  if (v instanceof Reference) {
    if (!v.base) {
      return ReferenceError(v.propertyName + &quot; is not defined&quot;);
    }
    return v.base[v.propertyName];
  }
  return v;
}[/js]

Dmitry.</description>
		<content:encoded><![CDATA[<p>@<b>Luiz</b></p>
<blockquote><p>1) In function <code>getValue()</code> I had to use return v.base instead of <code>v.base[v.propertyName]</code>;</p></blockquote>
<p>No, it&#8217;s not correct, should be nevertheless <code>v.base[v.propertyName]</code>.</p>
<p>The mistake you made is that you used not correct <code>base</code> component for e.g. <code>x</code> reference. You used <code>x</code> as the base, but it should be the global object. Once again, the base component of the reference is an object to which the property belongs. Global variable <code>x</code> belongs to global object, therefore global object is its base.</p>
<p>You have (wrong):</p>
<pre class="brush: jscript; title: ;">var x = 10;

// wrong
var xReference = new Reference(x,&quot;x&quot;);</pre>
<p>and it should be:</p>
<pre class="brush: jscript; title: ;">// global object -- we need it to
// use as a base of global variables!

var global = this;

var x = 10;

// base is &quot;global&quot; (not &quot;x&quot; as in your example),
// since variable &quot;x&quot; is a global variable

var xReference = new Reference(global, &quot;x&quot;);</pre>
<p>Also if you defined your own <code>ReferenceError</code> constructor (though, it&#8217;s a native built-in function), then for tests you may not throw it in <code>getValue</code>, but just return. So the complete code to test may look like this:</p>
<pre class="brush: jscript; title: ;">// global object
var global = this;

// global variable &quot;x&quot;
var x = 10;

// reference for global variable &quot;x&quot;:
// base: global, propertyName: &quot;x&quot;
var xReference = new Reference(global, &quot;x&quot;);

console.log(&quot;Type of 'x': &quot;, typeof x); // &quot;number&quot;
console.log(&quot;Value of 'x': &quot;, getValue(xReference)); // 10

// unresolved reference for non-existing
// variable &quot;y&quot; (i.e. base is null)

var yReference = new Reference(null, &quot;y&quot;);

console.log(&quot;Type of 'y': &quot;, typeof y ); // &quot;undefined&quot;
console.log(&quot;Value of 'y': &quot;, getValue(yReference)); // ReferenceError

function Reference(base, propertyName) {
  this.base = base;
  this.propertyName = propertyName;
}

function ReferenceError(error) {
    return &quot;Reference Error: &quot; + error;
}

function getValue(v) {
  if (v instanceof Reference) {
    if (!v.base) {
      return ReferenceError(v.propertyName + &quot; is not defined&quot;);
    }
    return v.base[v.propertyName];
  }
  return v;
}</pre>
<p>Dmitry.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Luiz</title>
		<link>http://dmitrysoshnikov.com/ecmascript/chapter-3-this/comment-page-1/#comment-5996</link>
		<dc:creator>Luiz</dc:creator>
		<pubDate>Wed, 02 Mar 2011 17:00:14 +0000</pubDate>
		<guid isPermaLink="false">http://dmitrysoshnikov.com/?p=415#comment-5996</guid>
		<description>Dmitry, 
thanks a lot for a detailed explanation. It&#039;s a lot of food for thought. 
Meanwhile this code fragment below runs OK with two exceptions:

1) In function &lt;code&gt;getValue()&lt;/code&gt; I had to use return v.base instead of &lt;code&gt;v.base[v.propertyName]&lt;/code&gt;;
2) for undefined var y  &lt;code&gt;ReferenceError&lt;/code&gt; is not called in the statements like:

[js]alert(y);[/js]

or 

[js]document.write(&quot;Value of &#039;y&#039;: &quot;, getValue(yReference));[/js]

[js]//========================================================//

var x = 10;

var xReference = new Reference(x,&quot;x&quot;);
document.write(&quot;Type of &#039;x&#039;: &quot;, typeof x ); 
document.write(&quot;Value of &#039;x&#039;: &quot;, getValue(xReference));

var yReference = new Reference(null,&quot;y&quot;); 
document.write(getValue(yReference));  // works, &quot;Reference Error&quot;
document.write(&quot;Type of &#039;y&#039;: &quot;, typeof y );    			//does not work
document.write(&quot;Value of &#039;y&#039;: &quot;, getValue(yReference)); //does not work
 
 
 
function Reference(base, propertyName) {
  this.base = base;
  this.propertyName = propertyName;
}

function ReferenceError(error) {
 document.write(&quot;Reference Error: &quot;, error);
} 

function getValue(v) {
  if (v instanceof Reference) {
    if (!v.base) {
      throw new ReferenceError(v.propertyName + &quot; is not defined&quot;);
    }
    return v.base;  //instead of v.base[v.propertyName];
  }
  return v;
}

//==================================================//[/js]

Thank you.
Luiz.</description>
		<content:encoded><![CDATA[<p>Dmitry,<br />
thanks a lot for a detailed explanation. It&#8217;s a lot of food for thought.<br />
Meanwhile this code fragment below runs OK with two exceptions:</p>
<p>1) In function <code>getValue()</code> I had to use return v.base instead of <code>v.base[v.propertyName]</code>;<br />
2) for undefined var y  <code>ReferenceError</code> is not called in the statements like:</p>
<pre class="brush: jscript; title: ;">alert(y);</pre>
<p>or </p>
<pre class="brush: jscript; title: ;">document.write(&quot;Value of 'y': &quot;, getValue(yReference));</pre>
<pre class="brush: jscript; title: ;">//========================================================//

var x = 10;

var xReference = new Reference(x,&quot;x&quot;);
document.write(&quot;Type of 'x': &quot;, typeof x );
document.write(&quot;Value of 'x': &quot;, getValue(xReference));

var yReference = new Reference(null,&quot;y&quot;);
document.write(getValue(yReference));  // works, &quot;Reference Error&quot;
document.write(&quot;Type of 'y': &quot;, typeof y );    			//does not work
document.write(&quot;Value of 'y': &quot;, getValue(yReference)); //does not work

function Reference(base, propertyName) {
  this.base = base;
  this.propertyName = propertyName;
}

function ReferenceError(error) {
 document.write(&quot;Reference Error: &quot;, error);
} 

function getValue(v) {
  if (v instanceof Reference) {
    if (!v.base) {
      throw new ReferenceError(v.propertyName + &quot; is not defined&quot;);
    }
    return v.base;  //instead of v.base[v.propertyName];
  }
  return v;
}

//==================================================//</pre>
<p>Thank you.<br />
Luiz.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dmitry A. Soshnikov</title>
		<link>http://dmitrysoshnikov.com/ecmascript/chapter-3-this/comment-page-1/#comment-5952</link>
		<dc:creator>Dmitry A. Soshnikov</dc:creator>
		<pubDate>Sun, 27 Feb 2011 15:38:40 +0000</pubDate>
		<guid isPermaLink="false">http://dmitrysoshnikov.com/?p=415#comment-5952</guid>
		<description>@&lt;b&gt;Luiz&lt;/b&gt;

Yes, as you correctly noted, it&#039;s a pseudo-code and just corresponds to the technical algorithm of the specification.

&lt;blockquote&gt;I am not sure how to interpret the &quot;base:global&quot; expression&lt;/blockquote&gt;

Well, for the &lt;code&gt;global&lt;/code&gt; we may use direct &lt;code&gt;this&lt;/code&gt; value in the global context as is &lt;a href=&quot;http://dmitrysoshnikov.com/ecmascript/chapter-3-this/comment-page-1/#this-value-in-the-global-code&quot; rel=&quot;nofollow&quot;&gt;mentioned&lt;/a&gt; in the appropriate section above. I.e.:

[js]var global = this;[/js]

Alternatively, in the browser environment, we can use &lt;code&gt;window&lt;/code&gt; binding to refer the global object (though, &lt;code&gt;this&lt;/code&gt; is more correct).

[js]var global = window;[/js]

&lt;blockquote&gt;GetValue() function is also kind of vaguely defined.

Can you please provide the same (or similar) code fragment in the form that can be actually executed (let’s say in Firebug debugger) and would be useful in exploring this internal machinery?&lt;/blockquote&gt;

Yeah, first, &lt;code&gt;GetValue&lt;/code&gt; described here is a pseudo-code reflection of the corresponding algorithm of the ES5/ES5 spec. Take a look on it -- &lt;a href=&quot;http://bclary.com/2004/11/07/#a-8.7.1&quot; rel=&quot;nofollow&quot;&gt;here&lt;/a&gt; (I used ES3 version when was writing this chapter, ES5 &lt;a href=&quot;http://es5.github.com/#x8.7.1&quot; rel=&quot;nofollow&quot;&gt;version&lt;/a&gt; is slightly modified, but the main part is the same).

A value of &lt;em&gt;Reference&lt;/em&gt; type as mentioned is a pair: &lt;code&gt;base&lt;/code&gt; and &lt;code&gt;propertyName&lt;/code&gt;. It can be presented as a constructor in plain JS:

[js]function Reference(base, propertyName) {
  this.base = base;
  this.propertyName = propertyName;
}[/js]

When interpreter meets a variable/function name (or in other words -- an &lt;em&gt;identifier&lt;/em&gt;), it &lt;em&gt;resolves&lt;/em&gt; it -- i.e. it &lt;em&gt;searches&lt;/em&gt; it in the &lt;em&gt;scope chain&lt;/em&gt;. This is a process of the &lt;a href=&quot;http://bclary.com/2004/11/07/#a-10.1.4&quot; rel=&quot;nofollow&quot;&gt;identifier resolution&lt;/a&gt; which is described in the &lt;a href=&quot;http://dmitrysoshnikov.com/ecmascript/chapter-4-scope-chain/&quot; rel=&quot;nofollow&quot;&gt;Chapter 4 Scope chain&lt;/a&gt;.

The thing interesting to us, is that the result of the identifier resolution mechanism is &lt;em&gt;always&lt;/em&gt; the &lt;em&gt;value of type reference&lt;/em&gt;.

If the identifier is found in the scope chain -- return the value of &lt;code&gt;Reference&lt;/code&gt; type with base containing &lt;em&gt;that&lt;/em&gt; &lt;a href=&quot;http://dmitrysoshnikov.com/ecmascript/chapter-2-variable-object/&quot; rel=&quot;nofollow&quot;&gt;variable object&lt;/a&gt; in which the identifier is found (scope chain as you probably know or will learn in the next chapter 4 is a chain of variable objects from different nested contexts). 

If the identifier is not found -- also the value of Reference type is returned, but with &lt;code&gt;base&lt;/code&gt; component equals to &lt;code&gt;null&lt;/code&gt;.

And then, if to transform our pseudo-code of &lt;code&gt;GetValue&lt;/code&gt; more closely to JS, we&#039;ll get:

[js]function getValue(v) {
  if (v instanceof Reference) {
    if (!v.base) {
      throw new ReferenceError(v.propertyName +
         &quot; is not defined&quot;);
    }
    return v.base[v.propertyName];
  }
  return v;
}[/js]

And if we have, e.g. global variable &lt;code&gt;x&lt;/code&gt;:

[js]var x = 10;[/js]

then after the identifier resolution mechanism, we get the following value of the &lt;code&gt;Reference&lt;/code&gt; type:

[js]var xReference = new Reference(global, &quot;x&quot;);[/js]

Notice, it&#039;s an &lt;em&gt;intermediate&lt;/em&gt; result of some calculation, it&#039;s &lt;em&gt;not&lt;/em&gt; the real value of &lt;code&gt;x&lt;/code&gt; yet, i.e. &lt;code&gt;10&lt;/code&gt;. To get exactly the &lt;em&gt;value&lt;/em&gt; of &lt;code&gt;x&lt;/code&gt;, already &lt;code&gt;getValue&lt;/code&gt; is applied:

[js]getValue(xReference); // 10[/js]

If in contrast we refer a non-existing variable from the code, e.g. &lt;code&gt;y&lt;/code&gt;, then as we said, we also get the value of Reference type, but with &lt;code&gt;base&lt;/code&gt; set to &lt;code&gt;null&lt;/code&gt;:

[js]var yReference = new Reference(null, &quot;y&quot;);[/js]

Such intermediate results (of the Reference type) are very important to JS. E.g. exactly via it the work of &lt;code&gt;typeof&lt;/code&gt; operator is explained. We won&#039;t get an error in the following code:

[js]alert(typeof y); // &quot;undefined&quot;[/js]

because &lt;code&gt;typeof&lt;/code&gt; &lt;em&gt;doesn&#039;t call &lt;em&gt;getValue&lt;/em&gt;&lt;/code&gt;, it just works &lt;em&gt;with the reference itself&lt;/em&gt;. However, we &lt;em&gt;do get&lt;/em&gt; the &lt;code&gt;ReferenceError&lt;/code&gt; if try to just alert the &lt;code&gt;y&lt;/code&gt;:

[js]alert(y); // &quot;ReferenceError&quot;: y is not defined[/js]

and now we know why -- because we provided such a behavior in the &lt;code&gt;getValue&lt;/code&gt;. Function &lt;code&gt;alert&lt;/code&gt; to display the value of the variable, should call &lt;code&gt;getValue&lt;/code&gt;:

[js]getValue(yReference); // y is not defined[/js]

And in the similar respect determination of &lt;code&gt;this&lt;/code&gt; value for function calls works. If on the left-hand side (LHS) &lt;em&gt;is&lt;/em&gt; the value of &lt;em&gt;Reference type&lt;/em&gt;, then &lt;code&gt;this&lt;/code&gt; is set to the &lt;em&gt;base&lt;/em&gt; of reference

[js]var foo = {
  bar: function () {
    alert(this);
  }
};
 
foo.bar(); // Reference, OK =&gt; foo[/js]

Grouping operator, the same as &lt;code&gt;typeof&lt;/code&gt; also &lt;em&gt;doesn&#039;t call&lt;/em&gt; our &lt;code&gt;getValue&lt;/code&gt;, which means either with, or without grouping operator -- we still have the reference value:

[js](foo.bar)(); // Reference, OK =&gt; foo[/js]
 
However, &lt;em&gt;assignment&lt;/em&gt; (the same as &lt;code&gt;alert&lt;/code&gt; above with &lt;code&gt;y&lt;/code&gt;) &lt;em&gt;does call&lt;/em&gt; &lt;code&gt;getValue&lt;/code&gt; and after its work we already have &lt;em&gt;not&lt;/em&gt; the value of Reference type, but the &lt;em&gt;function value&lt;/em&gt;, and as a result -- &lt;code&gt;this&lt;/code&gt; value is set to &lt;code&gt;null&lt;/code&gt; and then converted (in ES3) to &lt;em&gt;global object&lt;/em&gt;:

[js](foo.bar = foo.bar)(); // global[/js]

That is -- a shorter, but detailed description of these algorithms. Feel free to ask questions if is needed to clarify something. 

Dmitry.</description>
		<content:encoded><![CDATA[<p>@<b>Luiz</b></p>
<p>Yes, as you correctly noted, it&#8217;s a pseudo-code and just corresponds to the technical algorithm of the specification.</p>
<blockquote><p>I am not sure how to interpret the &#8220;base:global&#8221; expression</p></blockquote>
<p>Well, for the <code>global</code> we may use direct <code>this</code> value in the global context as is <a href="http://dmitrysoshnikov.com/ecmascript/chapter-3-this/comment-page-1/#this-value-in-the-global-code" rel="nofollow">mentioned</a> in the appropriate section above. I.e.:</p>
<pre class="brush: jscript; title: ;">var global = this;</pre>
<p>Alternatively, in the browser environment, we can use <code>window</code> binding to refer the global object (though, <code>this</code> is more correct).</p>
<pre class="brush: jscript; title: ;">var global = window;</pre>
<blockquote><p>GetValue() function is also kind of vaguely defined.</p>
<p>Can you please provide the same (or similar) code fragment in the form that can be actually executed (let’s say in Firebug debugger) and would be useful in exploring this internal machinery?</p></blockquote>
<p>Yeah, first, <code>GetValue</code> described here is a pseudo-code reflection of the corresponding algorithm of the ES5/ES5 spec. Take a look on it &#8212; <a href="http://bclary.com/2004/11/07/#a-8.7.1" rel="nofollow">here</a> (I used ES3 version when was writing this chapter, ES5 <a href="http://es5.github.com/#x8.7.1" rel="nofollow">version</a> is slightly modified, but the main part is the same).</p>
<p>A value of <em>Reference</em> type as mentioned is a pair: <code>base</code> and <code>propertyName</code>. It can be presented as a constructor in plain JS:</p>
<pre class="brush: jscript; title: ;">function Reference(base, propertyName) {
  this.base = base;
  this.propertyName = propertyName;
}</pre>
<p>When interpreter meets a variable/function name (or in other words &#8212; an <em>identifier</em>), it <em>resolves</em> it &#8212; i.e. it <em>searches</em> it in the <em>scope chain</em>. This is a process of the <a href="http://bclary.com/2004/11/07/#a-10.1.4" rel="nofollow">identifier resolution</a> which is described in the <a href="http://dmitrysoshnikov.com/ecmascript/chapter-4-scope-chain/" rel="nofollow">Chapter 4 Scope chain</a>.</p>
<p>The thing interesting to us, is that the result of the identifier resolution mechanism is <em>always</em> the <em>value of type reference</em>.</p>
<p>If the identifier is found in the scope chain &#8212; return the value of <code>Reference</code> type with base containing <em>that</em> <a href="http://dmitrysoshnikov.com/ecmascript/chapter-2-variable-object/" rel="nofollow">variable object</a> in which the identifier is found (scope chain as you probably know or will learn in the next chapter 4 is a chain of variable objects from different nested contexts). </p>
<p>If the identifier is not found &#8212; also the value of Reference type is returned, but with <code>base</code> component equals to <code>null</code>.</p>
<p>And then, if to transform our pseudo-code of <code>GetValue</code> more closely to JS, we&#8217;ll get:</p>
<pre class="brush: jscript; title: ;">function getValue(v) {
  if (v instanceof Reference) {
    if (!v.base) {
      throw new ReferenceError(v.propertyName +
         &quot; is not defined&quot;);
    }
    return v.base[v.propertyName];
  }
  return v;
}</pre>
<p>And if we have, e.g. global variable <code>x</code>:</p>
<pre class="brush: jscript; title: ;">var x = 10;</pre>
<p>then after the identifier resolution mechanism, we get the following value of the <code>Reference</code> type:</p>
<pre class="brush: jscript; title: ;">var xReference = new Reference(global, &quot;x&quot;);</pre>
<p>Notice, it&#8217;s an <em>intermediate</em> result of some calculation, it&#8217;s <em>not</em> the real value of <code>x</code> yet, i.e. <code>10</code>. To get exactly the <em>value</em> of <code>x</code>, already <code>getValue</code> is applied:</p>
<pre class="brush: jscript; title: ;">getValue(xReference); // 10</pre>
<p>If in contrast we refer a non-existing variable from the code, e.g. <code>y</code>, then as we said, we also get the value of Reference type, but with <code>base</code> set to <code>null</code>:</p>
<pre class="brush: jscript; title: ;">var yReference = new Reference(null, &quot;y&quot;);</pre>
<p>Such intermediate results (of the Reference type) are very important to JS. E.g. exactly via it the work of <code>typeof</code> operator is explained. We won&#8217;t get an error in the following code:</p>
<pre class="brush: jscript; title: ;">alert(typeof y); // &quot;undefined&quot;</pre>
<p>because <code>typeof</code> <em>doesn&#8217;t call </em><em>getValue</em>, it just works <em>with the reference itself</em>. However, we <em>do get</em> the <code>ReferenceError</code> if try to just alert the <code>y</code>:</p>
<pre class="brush: jscript; title: ;">alert(y); // &quot;ReferenceError&quot;: y is not defined</pre>
<p>and now we know why -- because we provided such a behavior in the <code>getValue</code>. Function <code>alert</code> to display the value of the variable, should call <code>getValue</code>:</p>
<pre class="brush: jscript; title: ;">getValue(yReference); // y is not defined</pre>
<p>And in the similar respect determination of <code>this</code> value for function calls works. If on the left-hand side (LHS) <em>is</em> the value of <em>Reference type</em>, then <code>this</code> is set to the <em>base</em> of reference</p>
<pre class="brush: jscript; title: ;">var foo = {
  bar: function () {
    alert(this);
  }
};

foo.bar(); // Reference, OK =&gt; foo</pre>
<p>Grouping operator, the same as <code>typeof</code> also <em>doesn't call</em> our <code>getValue</code>, which means either with, or without grouping operator -- we still have the reference value:</p>
<pre class="brush: jscript; title: ;">(foo.bar)(); // Reference, OK =&gt; foo</pre>
<p>However, <em>assignment</em> (the same as <code>alert</code> above with <code>y</code>) <em>does call</em> <code>getValue</code> and after its work we already have <em>not</em> the value of Reference type, but the <em>function value</em>, and as a result -- <code>this</code> value is set to <code>null</code> and then converted (in ES3) to <em>global object</em>:</p>
<pre class="brush: jscript; title: ;">(foo.bar = foo.bar)(); // global</pre>
<p>That is -- a shorter, but detailed description of these algorithms. Feel free to ask questions if is needed to clarify something. </p>
<p>Dmitry.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Luiz</title>
		<link>http://dmitrysoshnikov.com/ecmascript/chapter-3-this/comment-page-1/#comment-5946</link>
		<dc:creator>Luiz</dc:creator>
		<pubDate>Sun, 27 Feb 2011 04:09:58 +0000</pubDate>
		<guid isPermaLink="false">http://dmitrysoshnikov.com/?p=415#comment-5946</guid>
		<description>Hello Dmitry, 
First of all, thank you for so interesting series of articles. It is absolutely unique and definitely deserves to be published.

I am reading this chapter and specifically work on this pieces of code:
 
[js]var foo = 10;
function bar() {}
 
var fooReference = {
  base: global,
  propertyName: &#039;foo&#039;
};
 
var barReference = {
  base: global,
  propertyName: &#039;bar&#039;
};

 
function GetValue(value) {
 
  if (Type(value) != Reference) {
    return value;
  }
 
  var base = GetBase(value);
 
  if (base === null) {
    throw new ReferenceError;
  }
 
  return base.[[Get]](GetPropertyName(value));
 
}

 
GetValue(fooReference); // 10
GetValue(barReference); // function object &quot;bar&quot;[/js]

I realize that the code above is partially a pseudo-code, but I am still  trying to 
to modify it and  run to see in action what is actually going on here.

I am not sure how to interpret the &quot;base:global&quot; expression in both &lt;code&gt;fooReference&lt;/code&gt; and &lt;code&gt;barReference&lt;/code&gt; variables. &lt;code&gt;GetValue()&lt;/code&gt; function is also kind of vaguely defined. 

Can you please provide the same (or similar) code fragment in the form that can be actually executed (let&#039;s say in Firebug debugger) and would be useful in exploring  this internal machinery? This is a key point to understanding &quot;this&quot; so more details about the Reference type would be extremely useful.
Thanks in advance!</description>
		<content:encoded><![CDATA[<p>Hello Dmitry,<br />
First of all, thank you for so interesting series of articles. It is absolutely unique and definitely deserves to be published.</p>
<p>I am reading this chapter and specifically work on this pieces of code:</p>
<pre class="brush: jscript; title: ;">var foo = 10;
function bar() {}

var fooReference = {
  base: global,
  propertyName: 'foo'
};

var barReference = {
  base: global,
  propertyName: 'bar'
};

function GetValue(value) {

  if (Type(value) != Reference) {
    return value;
  }

  var base = GetBase(value);

  if (base === null) {
    throw new ReferenceError;
  }

  return base.[[Get]](GetPropertyName(value));

}

GetValue(fooReference); // 10
GetValue(barReference); // function object &quot;bar&quot;</pre>
<p>I realize that the code above is partially a pseudo-code, but I am still  trying to<br />
to modify it and  run to see in action what is actually going on here.</p>
<p>I am not sure how to interpret the &#8220;base:global&#8221; expression in both <code>fooReference</code> and <code>barReference</code> variables. <code>GetValue()</code> function is also kind of vaguely defined. </p>
<p>Can you please provide the same (or similar) code fragment in the form that can be actually executed (let&#8217;s say in Firebug debugger) and would be useful in exploring  this internal machinery? This is a key point to understanding &#8220;this&#8221; so more details about the Reference type would be extremely useful.<br />
Thanks in advance!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sojin</title>
		<link>http://dmitrysoshnikov.com/ecmascript/chapter-3-this/comment-page-1/#comment-4861</link>
		<dc:creator>sojin</dc:creator>
		<pubDate>Tue, 28 Dec 2010 14:53:35 +0000</pubDate>
		<guid isPermaLink="false">http://dmitrysoshnikov.com/?p=415#comment-4861</guid>
		<description>Perfect! thanks.</description>
		<content:encoded><![CDATA[<p>Perfect! thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dmitry A. Soshnikov</title>
		<link>http://dmitrysoshnikov.com/ecmascript/chapter-3-this/comment-page-1/#comment-4859</link>
		<dc:creator>Dmitry A. Soshnikov</dc:creator>
		<pubDate>Tue, 28 Dec 2010 10:39:09 +0000</pubDate>
		<guid isPermaLink="false">http://dmitrysoshnikov.com/?p=415#comment-4859</guid>
		<description>@&lt;b&gt;sojin&lt;/b&gt;, what a my mistake in that reply to &lt;b&gt;leoner&lt;/b&gt;! Thanks for figuring it out. I&#039;ve edited the previous reply with striking the wrong part of explanation.

It&#039;s not completely wrong of course; it&#039;s correct for exactly Firefox (in which console seems I was testing the code).

As you probably know Firefox defines a special type of functions which are called &lt;a href=&quot;http://dmitrysoshnikov.com/ecmascript/chapter-5-functions/#implementations-extension-function-statement&quot; rel=&quot;nofollow&quot;&gt;Function Statements&lt;/a&gt;. This extension allows to define functions conditionally in e.g. &lt;code&gt;if&lt;/code&gt;-blocks. In all other current implementations a function declaration is independent from the conditional &lt;code&gt;if&lt;/code&gt;-block.

[js]if (true) {
  function foo() {
    alert(1);
  }
} else {
  function foo() {
    alert(2);
  }
}

foo(); // 1 in FF, 2 - in other
[/js]

So in case of that example &lt;code&gt;f4&lt;/code&gt; as shown in the reply to &lt;b&gt;leoner&lt;/b&gt; is &lt;em&gt;hoisted&lt;/em&gt; to the top and saves the parent scope chain at the moment of its &lt;em&gt;definition&lt;/em&gt;. At this moment of course there is no that &lt;code&gt;{x: 5}&lt;/code&gt; added by the &lt;code&gt;with&lt;/code&gt;.

And in Chrome (and other) the behavior is &lt;em&gt;correct&lt;/em&gt;. It should be &lt;code&gt;20&lt;/code&gt; in the last call. But, in Firefox the behavior is &lt;em&gt;also correct&lt;/em&gt; -- depending on its &lt;em&gt;Function Statement&lt;/em&gt; extension (which behaves similarly in this respect to &lt;em&gt;Function Expressions&lt;/em&gt; as shown in that reply). And in FF it should be &lt;code&gt;50&lt;/code&gt; in the last call.

The future version of ECMAScript should standardize function statements, and then the correct result should be as in FF, i.e. &lt;code&gt;50&lt;/code&gt;. But now, both are correct. Since function declarations in blocks (which are function statements) are not defined by the current spec and are &lt;em&gt;extensions&lt;/em&gt; with their own behavior. Though, the behavior of FF seems more logical to me.

Dmitry.</description>
		<content:encoded><![CDATA[<p>@<b>sojin</b>, what a my mistake in that reply to <b>leoner</b>! Thanks for figuring it out. I&#8217;ve edited the previous reply with striking the wrong part of explanation.</p>
<p>It&#8217;s not completely wrong of course; it&#8217;s correct for exactly Firefox (in which console seems I was testing the code).</p>
<p>As you probably know Firefox defines a special type of functions which are called <a href="http://dmitrysoshnikov.com/ecmascript/chapter-5-functions/#implementations-extension-function-statement" rel="nofollow">Function Statements</a>. This extension allows to define functions conditionally in e.g. <code>if</code>-blocks. In all other current implementations a function declaration is independent from the conditional <code>if</code>-block.</p>
<pre class="brush: jscript; title: ;">if (true) {
  function foo() {
    alert(1);
  }
} else {
  function foo() {
    alert(2);
  }
}

foo(); // 1 in FF, 2 - in other
</pre>
<p>So in case of that example <code>f4</code> as shown in the reply to <b>leoner</b> is <em>hoisted</em> to the top and saves the parent scope chain at the moment of its <em>definition</em>. At this moment of course there is no that <code>{x: 5}</code> added by the <code>with</code>.</p>
<p>And in Chrome (and other) the behavior is <em>correct</em>. It should be <code>20</code> in the last call. But, in Firefox the behavior is <em>also correct</em> &#8212; depending on its <em>Function Statement</em> extension (which behaves similarly in this respect to <em>Function Expressions</em> as shown in that reply). And in FF it should be <code>50</code> in the last call.</p>
<p>The future version of ECMAScript should standardize function statements, and then the correct result should be as in FF, i.e. <code>50</code>. But now, both are correct. Since function declarations in blocks (which are function statements) are not defined by the current spec and are <em>extensions</em> with their own behavior. Though, the behavior of FF seems more logical to me.</p>
<p>Dmitry.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

