oop从子类调用父函数

时间:2017-01-28 11:50:09

标签: javascript

我知道这个问题很多时候才问过,但我仍然无法理解它。我正在使用javascrit oop,我需要从子类函数 ssh_client.connect( node_ip, username=local_settings.USERNAME, pkey=paramiko.RSAKey.from_private_key( cStringIO.StringIO(local_settings.RSA_KEY) ) ) 调用父类函数this.returnResult

this.fullArr

为什么function parantCls(){ this.sCus = []; this.aCus = []; this.response; this.returnResult = function(msg){ this.response = { result : msg }; return this; } } function resonse(){ parantCls.apply(this, arguments); this.fullArr = function(){ // call parent function parantCls.prototype.returnResult(this,'setting customField should be array not ' + typeof this.sCus); return this.response; } } resonse.prototype = new parantCls(); 无效。我也像这样使用parantCls.prototype.returnResult(this,'setting customField should be array not ' + typeof this.sCus);

call and apply

但仍然无法正常工作。有什么问题

1 个答案:

答案 0 :(得分:2)

如果使用原型链正确继承。

这应该有效

this.returnResult('setting customField should be array not ' + typeof this.sCus)

顺便说一下,你的继承看起来不对。使用这种格式。

var Subclass = function() {
    Superclass.call(this);
};

Subclass.prototype = Object.create(Superclass.prototype);
Subclass.prototype.constructor = Subclass;

Subclass.prototype.someMethod = function (value) {
    this.x = value;
};