0

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.

Dr. Frankenstein
  • 4,634
  • 7
  • 33
  • 48
  • 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
  • 1
    Wow, 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 Answers5

1

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
  • 24,126
  • 6
  • 49
  • 75
0

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
  • 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
0

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
  • 102,654
  • 32
  • 106
  • 127
0

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
  • 11,218
  • 4
  • 40
  • 61
-2

What IDE are you using? Try visual web developer express

Bablo
  • 906
  • 7
  • 18