"var" declarations shadowing properties from Window.prototype

Brendan Eich brendan at mozilla.com
Sun Aug 12 10:31:11 PDT 2012


Boris Zbarsky wrote:
> On 8/11/12 9:16 PM, Brendan Eich wrote:
>> As bz and others point out, the object detection w/ var pattern can
>> arise for operations, e.g. for requestAnimationFrame, using the same
>>
>>    var requestAnimationFrame = window.mozRequestAnimationFrame || ... ||
>> window.requestAnimationFrame;
>>
>> pattern. So WebIDL operations (JS methods) on the global would be
>> promoted to "own" too. They'd be configurable, if I recall correctly,
>> and either writable or replaceable. Do I have that right?
>
> This is unnecessary if we stick with the current var wording in 5.1.

Right. Unfortunately engines have shipped the erratum fix and it is 
helpful some times. We have a real dilemma. When var should shadow vs. 
not shadow depends on whether the detected property lives on the 
global's prototype chain. Thus the interest in moving everything down to 
be "own" properties on the global.

That way, function declarations clobber, and var leave alone. There's 
still a problem for getter-less attributes including indexedDB, so 
something *still* has to change there!

> If this _is_ done, that will mean hooking 
> EventTarget.prototype.addEventListener won't hook adds on the global, 
> which is a bit weird.

I think the primal sin (mine, again) is objects as scopes. But that 
can't be helped, so elaborating the global object's prototype chain, 
specifically to add expressiveness such as EventTarget monkeypatching, 
is going the wrong way. The global needs to be a flat object (only 
implementation-specific magic protos along a chain ending in 
Object.prototype).

And we still need to be careful about writability (get-only indexedDB 
point above).

/be


More information about the es-discuss mailing list