Why no prototypes without constructors?

J

Jeff Stewart

I'm curious about some of the machinery behind prototypal inheritance.
What's going on under JavaScript's hood that prevents me from assigning
properties to the prototype of an Object proper, rather than a
Function?

That is, why am I able to assign an object to a function's prototype
like so,

var f = function() {}
f.prototype = new BaseObject();

but the same procedure doesn't work (results in a meaningless
"prototype" key in the 'o' hash) in the usage below?

var o = new Object();
o.prototype = new BaseObject();
 
R

Richard Cornford

Jeff said:
I'm curious about some of the machinery behind prototypal inheritance.
What's going on under JavaScript's hood that prevents me from assigning
properties to the prototype of an Object proper, rather than a
Function?

That is, why am I able to assign an object to a function's prototype
like so,

var f = function() {}
f.prototype = new BaseObject();

but the same procedure doesn't work (results in a meaningless
"prototype" key in the 'o' hash) in the usage below?

var o = new Object();
o.prototype = new BaseObject();

When a function is the operand of the - new - operator the object
created has its internal [[Prototype]] property set to the value of the
function's (public) - prototype - property. It is the internal
[[Prototype]] property that points to the object that starts the
constructed object's prototype chain and is used when looking up
properties of the object whenever the object does not have those
properties itself. Because the object's [[Prototype]] property is
internal it cannot be modified by any code and so different values
cannot be assigned. The object referred to by the internal
[[Prototype]] property can be modified, and such changes then will
appear on the prototype chain of any object for which it is the
[[Prototype]].

Richard.
 
M

Martin Honnen

Jeff Stewart wrote:


but the same procedure doesn't work (results in a meaningless
"prototype" key in the 'o' hash) in the usage below?

var o = new Object();
o.prototype = new BaseObject();

Well you could do e.g.
Object.prototype.someProperty = someExpression
and then your o object created with new Object() "inherits" the
someProperty property from the prototype. But manipulating
Object.prototype is rather frowned upon.

In terms of the ECMAScript specification the prototype property/chain of
an individual object is internal and not exposed to user script.

Mozilla's JavaScript engine Spidermonkey however exposes the internal
prototype property to script as a property named __proto__ meaning if
you really think you need to manipulate the prototype chain of
individual objects then in Mozilla you can do e.g.

function Base () {
this.god = 'Kibo';
}

var obj = new Object();
obj.__proto__ = new Base();

alert(obj.god);

and it will alert 'Kibo'.


So there is a prototype for each object or rather a prototype chain,
only ECMAScript does not require that to be exposed to script.
 

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,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top