【发布时间】:2017-03-26 15:38:30
【问题描述】:
我有几个关于代码的问题。
1) 在 student 构造函数中,Person.call(this, firstName) 是否允许学生访问 person 的方法?还是会改变 this 的上下文?
2) 我假设 Student.prototype = Object.create(Person.prototype) 让我们可以访问 person 的方法和属性?
我很难理解调用方法。
var Person = function(firstName) {
this.firstName = firstName;
};
Person.prototype.walk = function(){
console.log("I am walking!");
};
function Student(firstName, subject) {
Person.call(this, firstName);
this.subject = subject;
};
Student.prototype = Object.create(Person.prototype); // See note belo
Student.prototype.constructor = Student;
Student.prototype.sayHello = function(){
console.log("Hello, I'm " + this.firstName + ". I'm studying "
+ this.subject + ".");
};
【问题讨论】:
标签: javascript object call