我正在学习javascript并感到困惑。这里有一个例子,例子如下: -
// define the Person Class
function Person() {}
Person.prototype.walk = function(){
alert ('I am walking!');
};
Person.prototype.sayHello = function(){
alert ('hello');
};
// define the Student class
function Student() {
// Call the parent constructor
Person.call(this);// <---- Confusion
}
// inherit Person
Student.prototype = new Person(); //<---- Confusion
// correct the constructor pointer because it points to Person
Student.prototype.constructor = Student;
// replace the sayHello method
Student.prototype.sayHello = function(){
alert('hi, I am a student');
}
// add sayGoodBye method
Student.prototype.sayGoodBye = function(){
alert('goodBye');
}
var student1 = new Student();
student1.sayHello();
student1.walk();
student1.sayGoodBye();
// check inheritance
alert(student1 instanceof Person); // true
alert(student1 instanceof Student); // true
现在,我<----
对这两行感到困惑( )。当我说Person.call(this);
时,这只是说明继承 Person 类的属性......对吗?
那么这是在做什么呢?
// inherit Person
Student.prototype = new Person(); //<---- Confusion
据我所知,.prototype
还继承了所有属性?