【发布时间】:2013-02-09 07:18:36
【问题描述】:
我正在学习面向对象的 Javascript 的某些方面。我遇到了这个sn-p
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");
我的问题是:为什么这个 sn-p 使用 Object.create(Person.prototype)?我们不应该简单地重置原型:
Employee.prototype = Person.prototype;
【问题讨论】:
标签: javascript prototype prototype-programming