On Jun 8, 8:45 pm, Adam <[email protected]> wrote:
> Very much.
>
> Not all browsers expose the real prototype of an object,

I think there is some confusion here between a constructor's public
prototype property and an object's private [[prototype]] property
(noting that all functions are objects, but not all objects are
functions).

Objects "inherit" from their private [[prototype]], which references
the public prototype of their constructor at the moment they were
constructed. Assigning a new object to the constructor prototype at
some later stage does not change the private [[prototype]] of objects
that have already been constructed by it. Newly constructed objects
(new instances) will have the new prototype as their private
[[prototype]].


> so you can't
> always navigate the chain.  The prototype property of a function/
> constructor is exposed, but that only gets you one layer deep.
>
> when you create a function:
>
> var a = function() {};
>
> You also create a prototype object auto-magically:

There is no "magic". ECMA-262 section 13.2 describes exactly what
happens during the creation of a function object. In summary, the
function's internal [[prototype]] is set to Function.prototype. The
function's public prototype is set to an object created by calling new
Object(). That object is given a constructor property that references
the function.

>
> a.prototype;
>
> This prototype has a magic reference back to the function

There is no magic, it is a public property that can be changed and
masked.

>
> a.prototype.constructor === a;
>
> So that's what you're seeing.  If you want to navigate the chain on an
> object, you need to use the __proto__ property:

In a typical case, an object's [[prototype]] will be its constructor's
prototype, whose [[prototype]] will be Object.prototype, whose
[[prototype]] is null.

If any other relationship is established, it is by native code so
while the objects in the chain can't necessarily be determined
afterward, how the chain was established should be discoverable by
looking at the code.

The __proto__ property is a proprietary Mozilla property that has been
copied by some other browsers.

>
> var b = new a();
> var c = b.__proto__;
> var d = c.__proto__;
> And so on until Object.prototype, and then finally null
>
> I wouldn't do it in production code though - IE will fail.

And probably others.


--
Rob

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