书上介绍了动态原型模式的代码如下:
function Person(name,age,job) {
//property
this.name = name;
this.age = age;
this.job = job;
//method
if(typeof this.sayName != "function") {
Person.prototype.sayName = function() {
console.log(this.name);
}
//Person.prototype = {
// sayName: function() {
// console.log(this.name);
// }
//} //这样的写函数的方式报错说没有 sayName 这个函数
}
}
当我用注释的 sayName 写法时会提示找不到 sayName 这个函数,为什么??
两种方式有什么异同吗?我记得书上提过第二种只是对第一种声明变量多的简便写法。