我不敢問這個問題,因爲已經有很多關於同一主題。瞭解Javascript原型繼承
我想了解使用approach-1
和approach-2
簡單的繼承
function Person(name, age) {
this.name = name || "de-name";
this.age = !!Number(age) ? Number(age) : 0;
}
Person.prototype.setName = function(name) {
this.name = name;
return this;
}
function Student(name) {
Person.call(this, name); // Copy instance properties of Person class
this.title = "Student";
}
// Suggested way
Student.prototype = Object.create(Person.prototype);
// Possible approach-1
// Student.prototype = Person.prototype;
// Possible approach-2
// Student.prototype = new Person();