Why do I never see the prototype property in JavaScript code I edit, from books and documentation I've read, it seems to be a cornerstone of the language.
-
Where are you **expecting** to see it? What code are you editing? – Pointy Jun 03 '10 at 16:54
-
I am expecting to see people assigning new methods to classes as in the case of the w3c example. – Dr. Frankenstein Jun 03 '10 at 17:21
-
1Wow, I think that's the worst thing I've seen from [w3schools](http://www.w3schools.com/jsref/jsref_prototype_array.asp).... the example doesn't really shows the real purpose of making an inherited property, and the description is just wrong and it only creates confusion: `Prototype is a global property which is available with almost all JavaScript objects.` – Christian C. Salvadó Jun 03 '10 at 17:24
5 Answers
Maybe because the majority of all javascript coders never cared to learn the basics of the language, and because loose approach allows for a lot of different ways of solving things.

- 24,126
- 6
- 49
- 75
The prototype
property only exists on Function
objects. Other objects do not have a prototype
property. It refers to the object that is used as the prototype for any object created by that function when used as a constructor.
function Thing() {
}
Thing.prototype = {
foo: "bar"
};
var t = new Thing();
window.alert(t.foo);

- 318,141
- 75
- 454
- 536
-
That's not really correct; all objects have a prototype that's *determined* by their constructor function. True, it's not available via an attribute called "prototype", but it's there. And of course any object *can* have an attribute called "prototype." – Pointy Jun 03 '10 at 16:57
-
That's why I was careful to refer to it as a "`prototype` property". I'm well aware that an object has a prototype that is determined by its constructor, and I mentioned it in my answer. It's a fair point that one could assign and use a `prototype` property on non-Function objects. – Tim Down Jun 03 '10 at 16:59
I don't know if an example is a solution, but this is an example of using a prototype.
Group.prototype['somethin'] must be defined, but Group.prototype exists when you create a new Group.
function Group(ob){
if(!ob || typeof ob!= 'object') ob= {};
for(var p in ob){
if(ob.hasOwnProperty(p)) this[p]= ob[p];
}
}
Group.prototype.merge= function(ob, force){
var tem, p, force= force!== false;
if(ob && ob.hasOwnProperty){
for(p in ob){
if(ob.hasOwnProperty(p)){
tem= this[p];
if(tem=== undefined || force) this[p]= ob[p];
}
}
}
return this;
}
Group.prototype.assignTo= function(ob, ob2, force){
return this.merge.call(ob, ob2, force);
}

- 102,654
- 32
- 106
- 127
in the work i've done, prototype
usually gets abandoned in favor of closure methods defined within the constructor so I can have private members in my javascript class. i'm aware that prototype
may serve a purpose when setting up inherited classes but i've never needed to approach that level of complexity.

- 11,218
- 4
- 40
- 61
-
For which the price you pay is greater memory use and slower constructors. – Tim Down Jun 03 '10 at 23:51
-
This sounds like the most likely answer to me. (difficult to choose a winner for this question) cheers – Dr. Frankenstein Jun 07 '10 at 08:55
-
Possibly it was difficult to choose a winner because the question was rather vague and therefore difficult to answer. – Tim Down Jun 07 '10 at 14:26