我一直在学习Javascript
中的继承,并且不会理解https://www.tutorialsteacher.com/javascript/inheritance-in-javascript
中的教程中的一行代码。
代码如下:
function Person(firstName, lastName) {
this.FirstName = firstName || "unknown";
this.LastName = lastName || "unknown";
}
Person.prototype.getFullName = function () {
return this.FirstName + " " + this.LastName;
}
function Student(firstName, lastName, schoolName, grade)
{
Person.call(this, firstName, lastName);
this.SchoolName = schoolName || "unknown";
this.Grade = grade || 0;
}
//Student.prototype = Person.prototype;
Student.prototype = new Person();
Student.prototype.constructor = Student;
var std = new Student("James","Bond", "XYZ", 10);
alert(std.getFullName()); // James Bond
alert(std instanceof Student); // true
alert(std instanceof Person); // true
我不理解的部分是注释行之后的行,它是:
Student.prototype = new Person();
据我所知,创建对象实例时,它的proto
属性指向该类的原型对象。
遵循此逻辑,不应编写以下代码:
Student.prototype = new Person().__proto__;
?
我非常感谢您作出澄清!