Thx a lot
o = {p: 42};
x = {__proto__: o};
print(x.p); // prints 42,
delegated from x to o, which is x's prototype
x.p = 43; // o.p
is still 42, but now x has a 'p' property hiding it
[[Parent]] is harder to see in action, especially as the __parent__ reflection property cannot be set (for security and safety reasons). The [[Parent]] link plays a crucial role, implementing static scoping rather than dynamic (caller) scoping, when there are multiple "global" or "top-level" objects, as in the HTML DOM:
In a script tag in foo.html, loaded into window w1:
var x = 3;
function f() { alert(x) }
In another script tag in bar.html, loaded into window w2:
var x = 4;
w1.f();
Per years of backward compatibility, dating from Netscape 2.0, and now per ECMA ed. 3 if you extend it to allow multiple "global objects" (Window objects), the call w1.f() in the second script must alert 3, not 4 (as it would if JS used dynamic scoping, where the caller's scope is used to parent the callee's activation).
The right x variable is found via the [[Scope]], also known in our engine as [[Parent]], internal property of the function object created for function f() {alert(x)} when the compiler processed that source function statement from foo.html. At that time, the compiler stored the window object reference w1 as the value of f's [[Parent]] property.
Later, when the script from bar.html called w1.f(), an activation object was created to scope f's invocation, and its scope chain link (again, [[Parent]]) was copied from f's [[Parent]]. The scope chain for the execution environment used during this invocation points to the activation object. One could diagram that scope chain thus:
scope-chain -[[Parent]]-> activation-for-f -[[Parent]]-> w1
Another example of [[Parent]] arises in the DOM's intrinsic event handlers, e.g.
<form id="myForm">
<input type="text" name="myText" value="">
<input type="button" name="myButton" onclick="myText.value
= 'Hi!'">
</form>
Notice that the onclick attribute's value refers to myText without any document.myForm. qualification to the left. The identifier myText is resolved by search the scope chain of the execution environment used when onclick is invoked. That scope chain looks like this (with --> standing for the -[[Parent]]-> arrow that I used above):
scope-chain --> activation-for-onclick --> myButton --> myForm --> document --> window
This deep scope chain allows intrinsic event handlers to use brief names, where unambiguous, for properties in the objects that enclose the event handler attribute's target object.
/be
Hope it helps
BugsB
Sent via Deja.com http://www.deja.com/
Before you buy.
Lu