小编Joo*_*n K的帖子

JavaScript 中继承类的原型对象如何等同于原始类的新实例?

我一直在学习 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 …
Run Code Online (Sandbox Code Playgroud)

javascript inheritance prototype proto

6
推荐指数
1
解决办法
51
查看次数

标签 统计

inheritance ×1

javascript ×1

proto ×1

prototype ×1