簡體   English   中英

什么意思是“向原型添加方法”

[英]what means “add method to prototype”

例如,我們已經有3個“類”(函數):水果<=蘋果<= GreenApple。 因此,Apple從Fruit繼承而GreenApple從Apple繼承。 (在實例上使用原型)。 因此,如果我正確理解:“ fruit.prototype”是一個對象,“ apple.prototype”是一個Fruit,“ greenApple.prototype”是“ Apple”? 因此,考慮到這一點,我有以下問題:

什么是“ Apple.prototype.getInfo”中的“ Apple.prototype” ???:

function Apple (type) {
    this.type = type;
    this.color = "red";
}

Apple.prototype.getInfo = function() {
    return this.color + ' ' + this.type + ' apple';
};

再說一次:對我來說,Apple的原型是一個Fruit(Apple.prototype == Fruit),但是在上面的代碼中,我們看到它使用了另一種方式,因此Apple.prototype是什么? 如果我們得到蘋果的新實例。

var greenApple = new GreenApple('apple1');

如果GreenApple類中沒有getInfo方法,將查看greenApple.prototype(Apple),但在Apple.prototype中聲明了getInfo(這是什么)?

最后:有什么區別:

Apple.prototype.getInfo = function(){*_*} 

Apple.getInfo = function() {*_*}

蘋果的原型是水果(Apple.prototype ==水果)

號“蘋果的原型是一個果”並不意味着“蘋果的原型是水果”

前者是指

Apple.prototype instanceof Fruit

后者意味着

Apple.prototype === Fruit

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM