在下面的代碼中,當我執行 console.log(teacher1.getDetails())時,屬性值顯示未定義,但當我執行 console.log(teacher1.personName) 或 console.log(teacher.mainSubject) 時,顯示屬性值。這里發生了什么?我錯過了什么?有人能解釋一下嗎?以及如何使它作業?
。let Person = function(personName, age) {
this.personName = personName;
this.age = age。
};
Person.prototype。 getDetails = function() {
return `Person Name:${this.personName}。年齡是${this.age}.`。
};
//Child建構式
let Teacher = function(personName, age, mainSubject) {
Person.call(this, personName, age)。
this.mainSubject = mainSubject;
};
Teacher.prototype = Object。 create(Person.>prototype>); //inheritance。
Teacher.prototype。 getDetails = function() {
return `${this.__proto__。 getDetails()} 主體是${this.mainSubject}.`; //可以選擇呼叫父方法。
};
let teacher1 = new Teacher("Sakib", 35, "物理")。)
console.log(teacher1.getDetails()); //invokes Teacher.getDetails()方法(孩子的方法)。
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
你需要像這樣在教師類中實作getDetails方法:
Teacher.prototype。 getDetails = function() {
return `${Person.prototype.getDetails. call(this)} 主體是${this.mainSubject}。
}
你的實作不起作用,因為接下來:你呼叫了關于父級背景關系的父級方法,而父級背景關系并沒有定義值,因為在教師建構式中,你定義了關于子級背景關系的值。
Person.call(this, personName, age)。
作業實體:
。let Person = function(personName, age) {
this.personName = personName;
this.age = age。
};
Person.prototype。 getDetails = function() {
return `Person Name:${this.personName}。年齡是${this.age}.`。
};
//Child建構式
let Teacher = function(personName, age, mainSubject) {
Person.call(this, personName, age)。
this.mainSubject = mainSubject;
};
Teacher.prototype = Object。 create(Person.>prototype>); //inheritance。
Teacher.prototype。 getDetails = function() {
return `${Person.prototype.getDetails. call(this)} 主體是${this.mainSubject}.`; /可選擇呼叫父方法。
};
let teacher1 = new Teacher("Sakib"。35, "物理")。)
console.log(teacher1.getDetails()); //invoke Teacher.getDetails()方法(孩子的方法)。
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
你的代碼中存在的問題:
this.__proto__.getDetails()沒有直接呼叫Person.prototype中定義的getDetails函式,因為在第一次呼叫getDetails()函式時,this.__proto__回傳Teacher.prototype。結果,this.__proto__.getDetails()呼叫了自己,即Teacher.prototype.getDetails()
當Teacher.prototype.getDetails()被第二次呼叫時,this.__proto__現在指向Person.prototype,因為this現在是Teacher.prototype,它的原型是Person.prototype。
第二次呼叫Teacher.prototype.getDetails()會呼叫Person.prototype.getDetails()函式
你需要系結this,以確保Person.prototype.getDetails()內的this的值是正確的
。
你不應該使用 __proto__ - 它已被廢棄。使用Object.getPrototypeOf()來獲取一個物件的原型
__proto__ - 它已經被廢棄了。
固定代碼:
。let Person = function(personName, age) {
this.personName = personName;
this.age = age。
};
Person.prototype。 getDetails = function() {
return `Person Name:${this.personName}。年齡是${this.age}.`。
};
let Teacher = function(personName, age, mainSubject) {
Person.call(this, personName, age)。
this.mainSubject = mainSubject;
};
Teacher.prototype = Object。 create(Person.prototype) 。
教師.prototype.constructor = 教師;
Teacher.prototype。 getDetails = function() {
const personPrototype = Object.getPrototypeOf(Object. getPrototypeOf(this)) 。
return `${personPrototype.getDetails. call(this)} 主體是${this.mainSubject}。
};
let teacher1 = new Teacher("Sakib"。35, "物理")。)
console.log(teacher1.getDetails());
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/320065.html
標籤:
