funciton Dog(){
this.tail = true;
}
var benji = new Dog();
var rusty = new Dog();
Dog.prototype.say = function(){
return ' Woof ! ';
}
>>>benji,say(); constructor->Dog()
""Woof !"
var instead = {paws: 4,hair:true}; constructor->Object()
>>>Dog.prototype =instead; constructor ->Object()
>>>benji.paws constructor->Dog()
"undefined"
>>>benji.say() constructor->Dog()
"Woof!"
var lucy = new Dog(); constructor->Object()
>>>lucy.say()
"TypeError:lucy.say is not a function"
>>>lucy.paws
4
解决办法:
Dog.prototype = {paws: 4, hair: true};
Dog.prototype.constructor = Dog;