I often see this pattern to define javascript objects
function Person(name) {
this.name = name;
}
Person.prototype.describe = function () {
return \"
There's nothing wrong with that code. This is supposedly what is meant:
function Person(name) {
this.name = name;
}
Person.prototype.age = 15; //<= adding a hardcoded property to the prototype
Now you will see this:
var pete = new Person('Pete'), mary = new Person('Mary');
pete.age; //=> 15
mary.age //=> 15
And most of the time, that's not what you want. Properties assigned to the prototype of a constructor are shared between all instances, properties assigned within the constructor (this.name) are specific for the instance.