我正在学习面向对象的 Javascript 的某些方面。我遇到了这个片段
var Person = function(firstName, lastName)
{
this.lastName = lastName;
this.firstName = firstName;
};
Object.defineProperties(Person.prototype, {
sayHi: {
value: function() {
return "Hi my name is " + this.firstName;
}
},
fullName: {
get: function() {
return this.firstName + " " + this.lastName;
}
}
});
var Employee = function(firstName, lastName, position) {
Person.call(this, firstName, lastName);
this.position = position;
};
Employee.prototype = Object.create(Person.prototype);
var john = new Employee("John", "Doe", "Dev");
我的问题是:为什么这个片段使用 Object.create(Person.prototype)?我们不应该简单地重置原型:
Employee.prototype = Person.prototype;