方法 1 正常,方法二没有声明 sayName 动态模型不能用字面量是吗?
//方式一
function Person(name){
this.name = name;
if(typeof this.sayName != "function") {
Person.prototype.sayName = function() {
alert(this.name);
}
}
}
var person1 = new Person("xiaoming");
person1.sayName();
//方式二
function Person(name){
this.name = name;
if(typeof this.sayName != "function") {
Person.prototype={ //这里不能用字面量是吗?
sayName:function() {
alert(this.name);
}
}
}
}
var person1 = new Person("xiaoming");
person1.sayName();