473,326 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Why no prototypes without constructors?

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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Michele Simionato | last post by:
So far, I have not installed Prothon, nor I have experience with Io, Self or other prototype-based languages. Still, from the discussion on the mailing list, I have got the strong impression that...
13
by: DevarajA | last post by:
If a function is visible everywhere, even out the file where it is declared and defined, why should i write prototypes? -- Devaraja (Xdevaraja87^gmail^c0mX) Linux Registerd User #338167...
7
by: junky_fellow | last post by:
Can a function have two different prototypes ? If not , then how can main() have two different prototypes ? int main(void) and int main argc(int argc, char *argv) I mean to say, if I declare...
20
by: Ari Krupnik | last post by:
scripts can add methods to the prototypes of builtin objects in JaavScript. I can assign functions to String.prototype.*, for instance. I want to add a method to Node, but when I try to execute...
73
by: Steph Barklay | last post by:
Hi, I'm currently taking a data structures course in C, and my teacher said that function prototypes are not allowed in any of our code. He also said that no professional programmers use function...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.