0
例如,JS代碼是:在這種情況下會生成多個相同的原型對象嗎?
function Fruit() {this.type = "fruit";};
function Apple() {this.name = "apple"; this.__proto__ = new Fruit();}
如果我請new Apple()
多次,將的new Fruit()
相同量來產生?還是取決於實施?
例如,JS代碼是:在這種情況下會生成多個相同的原型對象嗎?
function Fruit() {this.type = "fruit";};
function Apple() {this.name = "apple"; this.__proto__ = new Fruit();}
如果我請new Apple()
多次,將的new Fruit()
相同量來產生?還是取決於實施?
不,它不會。
原型被所有對象共享。蘋果的任何對象都會有一個類似於Fruit的原型,但每次都不會創建新的水果對象。
但設置_proto是一種不好的做法,而不是你能做到這一點作爲
Apple.prototype =的Object.create(Fruit.prototype),兩者確實有相同的效果
僅供參考proto reference