function Tt(){
this.name = "hello";
this.age = 88;
if(typeof this.say != "function"){
Tt.prototype = {
constructor: Tt,
say: function(){
return this.name;
}
};
}
}
var t = new Tt();
**console.log(t.say()); // Uncaught TypeError: Object #<Tt> has no method 'say' 这里为什么访问不到方法???**
// 如果这么做
function Tt(){
this.name = "hello";
this.age = 88;
if(typeof this.say != "function"){
Tt.prototype.say = function(){
return this.name;
}
}
}
var t = new Tt();
**console.log(t.say()); // 这样就没有问题!**
相关分类