There's nothing weird going on there, it's all defined and expected
behavior.

The first two cases are identical to each other, because window === this
when your code runs in the global scope. And as you noted, it is not an
error to attempt to dereference a nonexistent property of an object - you
simply get the undefined value.

The third case is not the same thing. The name 'foo' has not been defined to
be anything at all, so it's an error to use it all by itself like that.

As far as why it works the way it does, it's just because that's the way it
works, and how the standard is written. :-)

-Mike

On Fri, Feb 4, 2011 at 3:44 PM, mcot <[email protected]> wrote:

> I am currently seeing some weird behavior with the global object:
>
> console.log(window.foo);   // this returns undefined
> console.log(this.foo);        // this returns undefined
> console.log(foo);              // this is a reference error
>
> The Mozilla docs on Reference error simply state:
>
> A ReferenceError is thrown when trying to dereference a variable that
> has not been declared.
>
> I realize accessing a non existent property on an object should return
> undefined... which accounts for the first two cases... but whats going
> on in the third case?
>
> --
> To view archived discussions from the original JSMentors Mailman list:
> http://www.mail-archive.com/[email protected]/
>
> To search via a non-Google archive, visit here:
> http://www.mail-archive.com/[email protected]/
>
> To unsubscribe from this group, send email to
> [email protected]<jsmentors%[email protected]>
>

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to