javascript - oop 从子类调用父函数

标签 javascript

我知道这个问题以前问过很多次,但我仍然无法理解它。我正在使用 javascrit oop,我需要从子类函数 this.fullArr 调用父类函数 this.returnResult

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

parantCls.prototype.returnResult.call(this,'设置customField应该是数组而不是' + typeof this.sCus);

但还是不行。有什么问题

最佳答案

如果您使用原型(prototype)链正确继承。

这应该有效

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

关于javascript - oop 从子类调用父函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41909479/

相关文章:

javascript - 使用 jquery 隐藏另一个父级的子级

javascript - 我想从 JavaScript 对象列表中获取关联数组

java - 使用 LoadData 时在 WebView 中运行 Javascript

javascript - 如何从 javascript 创建 jQuery Mobile 按钮?

javascript - promise 链中 promise 之间的延迟

javascript - 添加到按钮的原型(prototype)javascript不显眼的点击事件

javascript - 无法读取 null 的属性 'spyOn' - 在 angularJS 单元测试中模拟 promise

javascript - Google Charts BarChart OnClick 或 OnSelect

javascript - Angular 脚本加载 : how to include all scripts in just one call

javascript - 向 jQuery datepicker() 函数添加时间