On Jun 8, 11:45 pm, "Frederick Polgardy" <[EMAIL PROTECTED]> wrote:
> On Sun, Jun 8, 2008 at 4:22 AM, T.J. Crowder <[EMAIL PROTECTED]> wrote:
>
> > Hi Fred,
>
> > No, not in standard JavaScript. The interpreter will work its way up
> > the scope chain looking for "Prototype"; if it reaches the global
> > object (which is always the root of the scope chain; the global object
> > in browser apps is the window object), it assumes the reference is a
> > property of that object. The property will have the value undefined.
No, it won't. There is a big difference between a property whose
value is undefined (as in a declared variable that has not been
assigned a value) and an indetifier that has not been defined at all
(i.e. not declared).
Read the ECMAScript spec, section 10.1.4. To summarise, if a property
with the same name as the identifier is not found on the scope chain,
then return "a value of type Reference whose base object is null and
whose property name is the Identifier".
Having resolved the identifier, it now must be evaluated, section 12.4
says:
1. Evaluate Expression.
2. Call GetValue(Result(1)).
3. Return (normal, Result(2), empty).
When it gets to step 2 and calls GetValue... Section 8.7.1 getValue
says:
1. If Type(V) is not Reference, return V.
2. Call GetBase(V).
3. If Result(2) is null, throw a ReferenceError exception.
Ta da.
> It seems like that would be the case based on JavaScript's lexical scoping
> rules, but in Firefox I get a JavaScript error ("ReferenceError: Prototype
> if not defined") when I try to alert(Prototype) or if(Prototype) { ... }.
>
> Didn't make sense to me either. :-)
Hopefully this order of posing makes more sense. :-)
It makes sense if you realise that undeclared variables are created as
global variables *only* when code is executed that assigns some value
to them.
e.g. the statement:
Prototype;
creates a reference error because when attempting to resolve the
identifier "Prototype", it can't be found and so returns a null
reference.
ECMAScript spec sections 12.4, and
Since JavaScript is a forgiving language, using:
Prototype = 'foo';
creates a Prototype property of the global object *when the statement
is executed* and the value 'foo' is assigned to it (sections 11.13.1
and 8.7.2 explain).
--
Rob
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---