js原型属性具有实时性
何为实时性
如:
function Person(){
this.name = "mike";
}
var p1 = new Person();
p1.name; // mike
Person.prototype.age = 12;
p1.age; // 12 实时挂在新属性
不靠谱的constructor
function Person(){
this.name = "mike";
}
Person.prototype.say = function(){
return this.name;
}
var p1 = new Person();
p1.say(); // "mike"
p1.constructor;
/*
ƒ Person(){
this.name = "mike";
}
*/
Person.prototype = {
sex: 'male'
}
p1.say();//"mike"
var p2 = new Person();
p2.say();// error;
p2.constructor; // ƒ Object() { [native code] }
改变了Person.prototype
constructor 也发生变化
但旧的p1 通过 __proto__
任然可以访问到 say
如何避免constructor丢失
在改变prototype后请将constructor指回原来的constructor
function Person(){}
Person.prototype = {};
Person.prototype.constructor = Person;