By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,814 Members | 2,162 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,814 IT Pros & Developers. It's quick & easy.

Why no prototypes without constructors?

P: n/a
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();

--
Jeff S.

Aug 25 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Jeff Stewart wrote:
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.

Aug 25 '06 #2

P: n/a


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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 25 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.