我正在學習面向對象的Javascript的一些方面。我碰到這個片段Javascript繼承:設置原型時調用Object.create
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;
一個偉大的【答案】(http://stackoverflow.com/questions/15040955/javascript-oop-and-prototypes-with-multiple-level-inheritance?answertab=active#從今天開始到此爲止。 – marekful 2013-02-23 20:16:31
哇。感謝那。爲學習書籤! :-) – 2013-02-23 20:21:16
[編號](http://stackoverflow.com/q/12500637/1048572) – Bergi 2013-02-23 22:44:16