2017-05-21 3 views
0

プロトタイプ継承関数作成パターンは2種類あります。protypal継承のメタオブジェクトは上記のES5を意味しますか?

Function.prototype.inherit = function(base) { 
    function Meta() {}; 
    Meta.prototype = base; 
    this.prototype = new Meta(); 
} 

Function.prototype.inherit = function(base) { 
    this.prototype = new base(); 
} 

元実装がある余分な何もするらしいしません!間にMeta関数がある場合、そのユースケースは何ですか? ES5で

function Base() { 
    console.log("This should not be called when inheriting!"); 
} 

// If you use `new Base()` as a prototype, an unwanted message is logged 

、これはObject.createとして内蔵されています:コンストラクタを呼び出すことによって発生する可能性がある副作用を避けるためにそこにあるMeta機能の

+0

1つの観測n形式アプローチは、ノード6.10ではもう機能しません。 –

答えて

1

ポイント

this.prototype = Object.create(base.prototype); 

とES6では、a classを使用できます:

class Derived extends Base { 
    // ... 
} 
関連する問題