2017-02-08 17 views
0

私は次のように子供のコンストラクタで親のメソッドにアクセスしようとしています:Javascriptを/ Node.jsの:ES5子クラスのインスタンスへのアクセス親クラスのメソッド

file1.js

var ParentClass = function(arg) { 
    this.arg = arg; 
    this.result = {}; 
}; 

ParentClass.prototype = { 
    constructor: ParentClass, 
    isActive: function(condition) { 
     return new Date(condition.valid).getTime() <= Date.now()) 
    } 
}; 

module.exports = ParentClass; 

file2.js

var ParentClass = require('file1'); 

var ChildClass= function(arg) { 
    ParentClass.apply(this, arguments); 
    this.init(); 
}; 

ChildClass.prototype = Object.create(ParentClass.prototype); 
ChildClass.prototype.constructor = ChildClass; 

ChildClass.prototype = { 
    init: function() { 
     this.result = this.arg.validity 
     .filter(function(elem) { 
      return this.isActive(elem) 
     }.bind(this)); 
    } 
} 

module.exports = ChildClass; 
このような場合の初期化

file3.js

var ChildClass= require('file2.js'); 
var instance = new ChildClass(param); 

は私に感謝

TypeError: this.isActive is not a function 
    at Object.<anonymous> (file2.js) 
    at Array.filter (native) 
    at Object.ChildClass.init (file2.js) 

ヘルプと説明を示します。ありがとうございました!

+0

が 'オブジェクトまたは配列をthis.arg.validity'ていますか? 'ParentClass.apply(this、arguments)'の目的は何ですか? – guest271314

+0

あなたは 'Object.create'を適切に使用していません。ドキュメントによると、**プロトタイプ**があなたが渡すオブジェクトと等しいオブジェクトを作成するので、それにアクセスするには 'ChildClass.prototype .__ proto__'と言うか、' ChildClass.prototype = Object.create(ParentClass.prototype).__ proto__'。また、 'ChildClass.prototype'を新しいオブジェクトとして再定義するのではなく、initメソッドを定義するだけです。 'ChildClass.prototype.init = function(){...}'と言っていますか? – mhodges

+1

@ guest271314 - それは、コンストラクタに渡された引数を持つ親クラスのコンストラクタを呼び出します。 – jfriend00

答えて

3

ChildClass.prototypeには2つの別個の割り当てがあります。一方は他方を無効にします。代わりに、最初にプロトタイプをObject.create()で初期化してから、プロトタイプ全体に代入するのではなく新しいメソッドを追加する必要があります。

これらは、相反する2つの文のとおりです。この問題を解決するために

ChildClass.prototype = Object.create(ParentClass.prototype); 

ChildClass.prototype = {...}; 

一般的な方法の1つは、既存のプロトタイプにあなたの方法をコピーするObject.assign()を使用することです:

Object.assign(ChildClass.prototype, { 
    init: function() { 
     this.result = this.arg.validity 
     .filter(function(elem) { 
      return this.isActive(elem) 
     }.bind(this)); 
    } 
}); 

これはあなたのそれぞれをコピーしますすでに存在するプロトタイプオブジェクトにメソッドをオーバーライドし、このメソッドは多くのメソッドがある場合によく使用されます。

また、単に(あなただけのいくつかのメソッドを持っているときに、より一般的に使用される)時に新しいメソッド1を割り当てることができます。

ChildClass.prototype.init = function() { 
    this.result = this.arg.validity.filter(function(elem) { 
     return this.isActive(elem) 
    }.bind(this)); 
} 
+0

@leela - ES6のクラス構文を使うと、書き込むのがずっと簡単になります(ただし、内部的には全く同じ結果が生成されます)。 – jfriend00

0

あなたのを上書きし、新しいオブジェクトとしてChildClass.prototypeを再定義しています数行前にObject.create()を使用して割り当て。代わりに、単純にinitメソッドを定義してください。

これを試してみてください:

var ParentClass = function(arg) { 
 
    this.arg = arg; 
 
    this.result = {}; 
 
}; 
 

 
ParentClass.prototype = { 
 
    constructor: ParentClass, 
 
    isActive: function(condition) { 
 
    return new Date(condition.valid).getTime() <= Date.now(); 
 
    } 
 
}; 
 

 
var ChildClass = function(arg) { 
 
    ParentClass.apply(this, arguments); 
 
    this.init(); 
 
}; 
 

 
ChildClass.prototype = Object.create(ParentClass.prototype); 
 
ChildClass.prototype.constructor = ChildClass; 
 
ChildClass.prototype.init = function() { 
 
    this.result = this.arg.validity 
 
    .filter(function(elem) { 
 
     return this.isActive(elem); 
 
    }.bind(this)); 
 
}; 
 

 

 
var instance = new ChildClass({ 
 
    validity: [{ 
 
    valid: "01/01/17" 
 
    }] 
 
}); 
 

 
console.log(instance);

関連する問題