views:

567

answers:

3

For the sake of an example, is this statement

window.Number.constructor.prototype.constructor();

read like a path?

C:\Users\Vista\Documents\Work\text.txt

From left to right

window:\Number\constructor\prototype\constructor()

where window is the root object, Number is an object inside window, constructor is an object inside Number, prototype is an object inside constructor and constructor() is an object inside prototype?

Just like in this statement

window.document.myForm.textBox.value;

which equals

[object].[object].[object].[object].1

where the objects aren't actually acting on each other?

OR

Are the actual values read from right to left, where each object is acting on the object directly to the left of it?

Where

window.Number.constructor.prototype.constructor();

equals

[object] . function Number() { [native code] } . function Function() { [native code] } . function prototype() { [native code] } . function anonymous() { }

as

window.Number(9.256).toFixed(2);

equals

[object].(9.256).(9.26);

where toFixed is a property that's using the return value of the Number object and the result is stored as a property of the window object?

As you can probably tell, I'm kinda tangled up over here :) Just having difficulty wrapping my head around the dot concept. I'm sure a background in Java would help, but unfortunately, I don't have one (yet).

A: 

Yes, or like a field in a struct in C. It's actually a bunch of hash tables or dictionaries. So your example

window.Number.constructor.prototype.constructor()

Is "The window object, an item named 'Number', which contains an item named 'constructor', which contains an item named 'prototype' --- which is where the methods are named --- which contains a method named constructor." That final () means "and treat this as a function with no arguments."

Charlie Martin
A: 

Left to right.

S.Lott
+1  A: 

Read from left to right. Each "thing" resolves to an object. Objects can have properties or functions. A property is another object, which can in turn have its own properties and functions. If it is a function, then to be legal syntax the function must return an object. Then the chained item to the right must be a property (or function) of that object.

A framework like jQuery works by having each of it's methods return a copy of itself so that methods can be chained together.

In your first example it is referring to a chain of object properties, except the last one which is a function. In the second, it invokes a function on the window object, which returns a Number object that has a toFixed() function.

tvanfosson