引用在控制台中显示错误的名称

引用在控制台中显示错误的名称

本文介绍了为什么我的 __proto__ 引用在控制台中显示错误的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我对迈克"的原型参考实例指向学生"但正如它的名字所显示的人",不知道为什么会这样.下面是我的代码和控制台屏幕截图:

My proto reference of "mike" instance points to "Student" but as its name shows "Person" , I don't know why is that. Here is my code and screenshot of console below:

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);

当我检查控制台时,它显示了 Person:

When I check on console it shows Person:

推荐答案

Student.prototype = Object.create(Person.prototype)在本例中,您正在创建一个新对象并将其设为 Student.prototype 的值.新对象 StudentPerson.prototype 作为其原型.

Student.prototype = Object.create(Person.prototype)In this case, you are creating a new object and make it the value of Student.prototype. The new object Student has Person.prototype as its prototype.

这篇关于为什么我的 __proto__ 引用在控制台中显示错误的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 14:49