當我們使用「new」運算符爲javascript中的對象創建實例時會發生什麼?並且,在創建過程中,何時分配了構造函數的原型? 我嘗試動態地分配在構造一個新的原型,但結果頗爲怪異:在構造函數中動態賦值原型不起作用
function Person(name, age){//the super class constructor
this.name = name;
this.age = age;
if (typeof this.sayName != "function") {//set the function in prototype so Person's instance or subType can share just one instance of function(since function in javascript is Object indeed)
Person.prototype.sayName = function(){
console.log("my name is ", this.name);
}
}
}
//if I assign Student's prototype here, not in its constructor, it will be OK, but why not encapsulate it within the constructor if I can?
//Student.prototype = new Person();
function Student(name, age, school, grade){//the subType constructor
Person.call(this, name, age);//get the super class property
this.school = school;//init the subType property
this.grade = grade;
if (!(Student.prototype instanceof Person)) {//just assign the prototype to Student for one time
Student.prototype = new Person();
}
}
let person1 = new Student("Kate", 23, "Middle school", "3.8");
let person2 = new Student("Gavin", 23, "Middle school", "3.8");
let person3 = new Student("Lavin", 23, "Middle school", "3.8");
person1.sayName();//Uncaught TypeError: person1.sayName is not a function
person2.sayName();//my name is Gavin
person3.sayName();//my name is Lavin
- 自「sayName()」可以分配到人的原型,我們可以得出結論,原型已經準備好當構造函數代碼執行
- 基於第1點,在構造函數Student()中,爲什麼我無法替換原始原型?(person1將找不到sayName函數)
- 當構造函數的原型被分配了嗎?我可以在哪裏更換默認原型?
@smarber,感謝您的解釋和鏈接「http://stackoverflow.com/a/17393153/1507546」。但實際上我迷惑了爲什麼person1無法獲得sayName方法,而person2和person3可以?當我們新建一個對象時,我對後面的場景感到好奇。 – blueGhost
你應該評論他的帖子,而不是你的帖子,否則他不會得到通知。 :) –