Here I found information hot to works scope chain:
...before we go to the parent link, first proto chain is considered.
Notice, that not in all implementations the global object inherits from the Object.prototype. The behavior described on the figure (with referencing “non-defined” variable x from the global context) may be tested e.g. in SpiderMonkey.
I use Firefox browser for testing, but, when I set global variable x
, and set to Object.prototype
property x
and do a()
I've got 4. Why, if first we go to proto
?
var x = 1;
Object.prototype.x = 2;
var a = function () {
y = 3;
return this.y + this.x;
};
a.x; // 2
a(); // 4
You should've cited the beginning of that paragraph, not the end:
At code execution, a scope chain may be augmented using
with
statement andcatch
clause objects.
So what your sentence refers to are only object environment records - scope objects in which variables live. Those are typically imaginary, described for specification purposes only (and can only be observed in Rhino), but the with
statement makes an exception and prepends an actual JS object to the scope chain.
If you check out the example, you will find the with
statement right there. The gist:
Object.prototype.x = "inherited object property";
var x = "parent scope variable";
with ({}) {
alert(x); // you bet what it yields
}
When I set global variable
x
, and setObject.prototype.x
property, why doesn't it get the prototype value?
By using this
in a function that is called plainly (a()
), you are referring to the global object. It is indeed the object for the object environment record of the global scope, which means that all global variables live there as properties.
Now why does accessing x
on that object (regardless whether by variable x
or property this.x
) bring up 1
not 2
? Because the global variable with the value 1
is an own property of the object. The prototype chain will only be traversed if the property is not already found!