More fun with undefined

T.J. Crowder tj at crowdersoftware.com
Fri Jun 15 06:52:30 PDT 2012


On 15 June 2012 14:34, Aymeric Vitte <vitteaymeric at gmail.com> wrote:

> Example :
>
> console.log(a);//Reference error, GetBase returns undefined
>
> console.log(window.a);//undefined
>
> --> does not seem very logical, no ?
>

To me this would be a big step backward, after the very large stride
forward this group made in ES's strict mode of making _assigning_ to an
unresolvable reference an error rather than an implicit creation of a
property on the global object.

For one thing, how is the engine to know that the `a` in question was meant
to be `window.a`? Maybe I just forgot to put `var a` in the current scope.
(In fact, that's usually what it is when I get this error.)

Scope is not the same as an object (although of course, the scope chain is
conceptually made up of binding objects). If I refer to `a` in my code and
`a` has never been declared, that's a bug, and as I haven't told it, the
engine has no way of knowing what level in the scope chain I intended `a`
to be in. If I access the `a` property of an object, and the property has
never been defined, that could just be lazy initialization; the engine
knows that I'm talking about that specific object (or its prototypes),
because I've told it what object to look at.

Separately:

(ie a way that this works : if (a.b.c.d) {} when a,a.b, etc are not set)


Making the initial `a` evaluate to `undefined` wouldn't make that work:
Instead of the ReferenceError, you'd get a TypeError (cannot read property
`b` of undefined).

-- T.J.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120615/fa584e07/attachment.html>


More information about the es-discuss mailing list