我想弄清楚'this'在我最後一個函數(Mamamal.prototype.haveBaby)中引用了什麼?這是什麼價值?
var Mammal = function(name){
this.name = name;
this.offspring = [];
};
// var myMammal = new Mammal('Joe');
Mammal.prototype.sayHello = function(){
return 'My name is ' + this.name + ", I'm a Mammal";
};
Mammal.prototype.haveBaby = function(){
debugger;
var childName = "Baby " + this.name;
baby = new this.constructor(childName); //new Cat OR new Mammal
baby.name = childName;
this.offspring.push(baby);
return baby;
};
我不知道爲什麼語法
baby - new this.constructor(childName);
是this
Mammal.prototype?(那麼構造函數,因此這將是Mammal.prototype.constructor(childName);
這就是我知道如何設置構造函數的唯一途徑。Mammal.constructor是行不通的。