"var" declarations shadowing properties from Window.prototype

Domenic Denicola domenic at domenicdenicola.com
Sat Aug 11 23:36:08 PDT 2012

-----Original Message-----
From: es-discuss-bounces at mozilla.org [mailto:es-discuss-bounces at mozilla.org] On Behalf Of Brendan Eich
Sent: Saturday, August 11, 2012 22:57

> As noted, they started out that way 17 years ago. I think WebIDL and interface-based method definition made onload, e.g., predefined on window objects, or more recently on Window.prototype. Was this useful? 
> Was it intended specifically (for window, not just intended generally due to WebIDL's uniform rules for binding its definitions in JS)?

As a developer, the fact that the properties already exist and are set to null is useful for feature-detection. E.g. any of the following will test for the presence of cross-document messaging:

"onmessage" in window
window.onmessage !== undefined
window.onmessage === null // assuming no badly-behaved third-party scripts

It is also useful strictly from a development POV, because typing `window.on` in the Firefox or Chrome console gives a list of supported events.

FWIW, my mental model is probably closest to these being non-magical own data properties, pre-initialized to null, that the browser will read from and use when it fires the corresponding event. They don't make much sense as accessors to me.

More information about the es-discuss mailing list