function Person1(name,age){ this.name = name; this.age = age; if (typeof this.sayName != 'function') { Person1.prototype.sayName = function(){ console.log(this.name); } } }
function Person1(name,age){ this.name = name; this.age = age; if (typeof this.sayName != 'function') { Person1.prototype = { sayName:function(){ console.log(this.name); } } } }
上边和下边不一样,下边会断开以前的原型的指针,因为{}就是一个对象