tags:

views:

107

answers:

6

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.

A: 

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);
Tim Down
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
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
A: 

What IDE are you using? Try visual web developer express

Bablo
-1, this is unrelated.
Matti Virkkunen
Oh my! I didn't quite read the OP properly :(
Bablo
A: 

check this http://stackoverflow.com/questions/383172/javascript-correct-prototype-chain-for-function

Space Cracker
wow - theres a lot of info there!
Dr. Frankenstein
+1  A: 

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.

jishi
this is what I am suspicious of
Dr. Frankenstein
A: 

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);
}
kennebec
A: 

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.

lincolnk
For which the price you pay is greater memory use and slower constructors.
Tim Down
This sounds like the most likely answer to me. (difficult to choose a winner for this question) cheers
Dr. Frankenstein
Possibly it was difficult to choose a winner because the question was rather vague and therefore difficult to answer.
Tim Down