For newbies: a diagram showing the difference between scope and prototypechains

R

Richard Cornford

java said:
Those new to javascript will find the following diagram
interesting. It shows the difference between prototype
and scope chains in JS.

http://mollypages.org/misc/jsscope.mp


On that diagram the statement: "All primitive (integers, booleans etc)
or reference (strings, objects) variables are located via their scope
chins." - is inaccurate and misleading. Javascript has no integer
primitives, only numbers (which are IEEE double precision floating point
numbers). Strings are primitive values in javascript not objects. And,
in context, "via their scope chain" implies that the scope chain belongs
to the variable, while only execution contexts and function objects
possess scope chains (and only the former are significant in Identifier
resolution).

The statement: "Once the variable is found, if it happens to be an
object, then:

that_object.x

is found via that objects prototype chain." - fails to observe that if
the "object" has the property itself its prototype chain does not come
into consideration (instead implying that only the prototype chain is
used).

And "Once the variable is found" means that the variable will always be
found to be a named property of some object, so the "if it happens to be
an object" is redundant or misleading or very badly worded. This is also
true of the statement "Once an object is found(via scope), to find a
property IN that object (that is using "."), that property is found vial
the object's prototype chain, but in that case an impression reinforced
by the dotted line with the arrow on the end circumventing the
"myobject" box and intersecting the arrow to the "proto" box. That
second statement also appears to be disregarding the possibility of
using bracket notation property accessor in addition to dot notation
property accessor.

On the whole the diagram is too simple as an explanation of the scope
chain and poorly explained with regard to the prototype chain.

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top