为什么我的 __proto__ 参考在控制台中显示错误的名称?

2023-12-03

My proto“mike”实例的引用指向“Student”,但正如其名称显示“Person”,我不知道为什么会这样。下面是我的代码和控制台的屏幕截图:

const Person = function (firstName,birthYear) {
   this.firstName = firstName;
   this.birthYear = birthYear;
}

Person.prototype.calcAge = function () {
   console.log(2037 - this.birthYear);
}


const Student = function (firstName, birthYear, course){
   Person.call(this,firstName,birthYear);
   this.course = course;
};


Student.prototype = Object.create(Person.prototype)

Student.prototype.constructor = Student;

Student.prototype.introduce = function () {
   console.log(`My name is ${this.firstName} and ,I study ${this.course}`);
}

const mike = new Student('Mike',2020, 'Computer Science');

console.log(mike);

当我检查控制台时,它显示人员:

Console Screenshot


Student.prototype = Object.create(Person.prototype)在这种情况下,您将创建一个新对象并使其值为Student.prototype。新对象Student has Person.prototype作为它的原型。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么我的 __proto__ 参考在控制台中显示错误的名称? 的相关文章

随机推荐