Javascript中的继承
我正在学习Javascript中的继承概念,我正在学习的教程使用以下代码:Javascript中的继承,javascript,inheritance,Javascript,Inheritance,我正在学习Javascript中的继承概念,我正在学习的教程使用以下代码: // define the Student class function Student() { // Call the parent constructor Person.call(this); } // inherit Person Student.prototype = new Person(); // correct the constructor pointer because it points
// define the Student class
function Student() {
// Call the parent constructor
Person.call(this);
}
// inherit Person
Student.prototype = new Person();
// correct the constructor pointer because it points to Person
Student.prototype.constructor = Student;
我的问题是,为什么需要调用父构造函数
Person.call(this)
,并将Student
原型设置为一个新的Person
对象(即Student.prototype=new Person();
) 有两个独立的问题需要处理
第一个是确保新的Student
对象继承自Person
对象。这就是为什么要做Student.prototype=newperson()
。这使得Student.prototype
成为Person
,因此Student
对象将继承该对象继承的任何内容(从Person.prototype
)
第二个是将Person
构造函数中的任何行为应用于任何新的Student
对象。这就是为什么你要打电话给某人。打电话给(这个)。如果Person
构造函数没有任何修改新对象的代码,从技术上讲这是不需要的,但是如果您以后向Person
添加一些代码,那么这样做仍然是一个好主意
请注意,在设置继承时,最好执行以下操作:
Student.prototype = Object.create(Person.prototype)
…并填充
对象。如果需要,创建。这样,您实际上不需要调用Person
构造函数来获取从Person.prototype
继承的新对象。同样,有时不是问题,但有时构造函数中存在设置继承时不希望出现的副作用。在进行一些测试后,据我所知,对象的原型及其定义函数中的属性声明是分开的。但是,当构造新对象时,构造函数将从定义函数和所创建对象的原型中提取属性
比如说,
function Person()
{
this.name = "james";
this.age = "shfifty-five";
}
console.log(Person.prototype.name); // prints "undefined", showing how declaring function Person() and Person.prototype are separate.
Person.prototype.gender = "male";
var human = new Person();
console.log(human.name); // prints "james", showing how the object took properties from declaring function Person().
console.log(human.gender); // prints "male", showing how the object took properties from object prototype Person.prototype.
function Student()
{
this.gpa = 4.00;
}
Student.prototype = new Person();
Student.prototype.constructor = Student;
var myStudent = new Student();
console.log(myStudent.name); // prints "james"
console.log(myStudent.gender); // prints "male"
/*from above two lines, inheritance holds even though 1) Person() defining function was not called inside Student() defining function
and 2) Person() defining function declares properties of Person object.*/
console.log(myStudent.gpa); // prints "4"
如您所见,在这种情况下,定义函数Person确实会更改其对象的属性。但是仍然没有必要在Student的构造函数中调用Person函数,因为new Person()
构造函数将从Person.prototype
和function Person()
定义函数中提取属性 我只是好奇。。。设置原型构造函数的原因是什么<代码>Student.prototype.constructor=Student代码>+1用于删除new Person()
的修复程序,如果父构造函数具有参数,则可能会出现问题。同样在构造函数中,也可以发送参数。@Polaris878:这是因为默认的Student.prototype
对象有一个.constructor
属性指向Student
函数。。。但是该对象正在被替换,因此如果需要.constructor
属性,则需要手动将其添加到新对象中。但是我们不是有效地调用了构造函数两次吗?Person.call(this)和new Person()之间有什么区别?为什么在对象的原型中没有反映对象定义功能中对对象的更改?@voltair:newperson
用于创建新对象。Person
函数中的新对象由this
引用。在执行Person.call(this)
时,我们将获取当前的this
,这是新的Student
对象,并手动将其设置为Person
函数的this
值。这样说吧。。。在JavaScript中,继承几乎与构造函数无关。当一个对象从另一个对象继承时,不会自动调用构造函数。构造函数只是建立两个对象之间原型关系的一种奇怪的、令人困惑的方式。。。每个新的Student
对象将继承Student.prototype
对象的属性。在您的示例中,当您执行Student.prototype=new Person()
时,会向正在分配的Person
对象添加一些属性,因此这些属性自然会被继承。但是如果在Person
构造函数中分配的值不是静态的呢?如果没有对新的Student
对象调用Person
,它们将始终引用在原型对象上设置的相同属性值。如果您确实希望继承静态值,那么应该将它们放在Person.prototype
上。