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()); // 这样就没有问题!**
小白,不知道为什么这样??希望帮忙解释一下~
It creates a new object.
It sets the constructor property of the object to Vehicle.
It sets up the object to delegate to Vehicle.prototype.
It calls Vehicle() in the context of the new object.