我正在查看使用原型的繼承示例,如下所示:
function Person(name){
this.name = name
}
function Student(name){
Person.call(this, name)
}
Student.prototype = Object.create(Person.prototype)
Student.prototype.constructor = Student
let jim = new Student("Jim")
我的問題是,為什么需要使用Object.create(Person.prototype)為什么不簡單地設定原型Person.prototype?
uj5u.com熱心網友回復:
如果你這樣做
Student.prototype = Person.prototype;
下一步嘗試擴展它:
Student.prototype.a = function(....
你也會受到影響Person.prototype(因為它在分配后是完全相同的物件)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/363214.html
標籤:javascript 原型链
上一篇:減少總和并減去嵌套陣列
