![](/img/trans.png)
[英]How to list "new" member of an JavaScript object that is not instance/static member from its base or inherited class?
[英]How does the prototype object of an inherited class equal a new instance of the original class in JavaScript?
我一直在学习JavaScript中的inheritance,在https//www.tutorial-javascriptsteacher.com/
代码如下:
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();
据我了解,当创建 object 实例时,其__proto__
属性指向该类的原型 object。
按照这个逻辑,代码不应该是:
Student.prototype = new Person().__proto__;
?
我真的很感激一些澄清!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.