On 2/4/11, mcot <atm1...@gmail.com> 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
As expected, getting a property off an object, the prototype chain is
searched. When that happens, if the property is not found, then
`undefined` results.

But with scope chain resolution, when the property is not resolved, an
error results.

| 11.1.2   Identifier Reference
| An Identifier is evaluated by performing Identifier Resolution
| as specified in 10.3.1. The result of evaluating an Identifier
| is always a value of type Reference.

...

| 10.3.1   Identifier Resolution
| Identifier resolution is the process of determining the binding of an
| Identifier using the LexicalEnvironment of the running execution context.

> console.log(foo);              // this is a reference error
>

Identifier `foo` is resolved to a Reference with null as the base
object. In ES5, it looks as if it is a Reference with base object as
`undefined`. With either spec, the result will be the same
ReferenceError. ES5 gets a little fancy with the explanation.

> The Mozilla docs on Reference error simply state:
>
> A ReferenceError is thrown when trying to dereference a variable that
> has not been declared.
>
They mean that when you try and get the value of an Identifier in a
PrimaryExpression and the Identifier is not resolved, then the base
object is null (or now `undefined`) that the attempt to get at the
value is going to result in a ReferenceError.

So when you have an Expression like:

console.log(foo);

or even just a PrimaryExpression:

foo // a PrimaryExpression.

Identifier `foo` must be first resolved. The base object for that
value is null (or so"undefined") and the when the expression is
evaluated, it tries to get the value, and then finds the base object
is null and throws a ReferenceError is thrown.

| 8.7.1   GetValue (V)
|
|   1. If Type(V) is not Reference, return V.
|   2. Let base be the result of calling GetBase(V).
|   3. If IsUnresolvableReference(V), throw a ReferenceError exception.

...

| IsUnresolvableReference(V). Returns true if the base value
| is undefined and false otherwise.

The MDC docs might not say it, and you didn't ask, either, but in
strict code, assignment to undeclared Identifier will result in
referenceerror too.

> 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?

An Identifier resolution was performed on the scope chain. Just
remember the difference when getting a property fails: With object
properties - the prototype chain is used and the result is undefined.
With unqualified Identifiers, the scope chain is searched in the
result is ReferenceError.
-- 
Garrett

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/jsmentors@jsmentors.com/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/jsmentors@googlegroups.com/

To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com

Reply via email to