2017-02-08 97 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。一個會覆蓋另一個。相反,您需要首先使用Object.create()來初始化原型,然後您需要向其中添加新的方法,而不是分配給整個原型,從而替換剛纔放置的所有東西。

這是兩個相互矛盾的聲明:

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

ChildClass.prototype = {...}; 

一個解決這個問題是使用Object.assign()複製你的方法到現有原型常見的方式:

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

這將複製您的每一個方法轉移到已經存在的原型對象上,並且這種方法在有很多方法時更常用。

你也可以指定新的方法,每次一個(當你只有幾個方法比較常用):

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);