Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript oop从子类调用父函数_Javascript - Fatal编程技术网

Javascript oop从子类调用父函数

Javascript oop从子类调用父函数,javascript,Javascript,我知道这个问题以前问过很多次,但我还是无法理解。我使用的是javascrit oop,我需要调用子类函数this.fullArr中的父类函数this.returnResult function parantCls(){ this.sCus = []; this.aCus = []; this.response; this.returnResult = function(msg){ this.response = { r

我知道这个问题以前问过很多次,但我还是无法理解。我使用的是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(这个,'settingcustomfield应该是数组而不是'+typeof this.sCus)不工作。im也像这样使用
调用和应用

parantCls.prototype.returnResult.call(这个,'settingcustomfield应该是数组而不是'+typeof this.sCus)


但还是不行。如果使用原型链正确继承,问题是什么

这应该行得通

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

如果使用原型链正确继承

这应该行得通

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

根据您调用
fullArr
的方式,您最后的代码(
parantCls.prototype.returnResult.call(this,…
)应该可以工作(尽管它不必要地复杂,因为您可以像gzc指出的那样只使用
this.returnResponse
),因此:请使用可运行的堆栈片段更新您的问题(
[]
工具栏按钮)。根据您调用
fullArr
的方式,您最后的代码(
parantCls.prototype.returnResult.call(this,…
)应该可以工作(尽管它不必要地复杂,因为您可以像gzc指出的那样使用
this.returnResponse
)。因此:请使用可运行的堆栈片段更新您的问题(使用
[]
工具栏按钮)。“顺便说一句,您的继承看起来是错误的。”这没有错,因为他们没有使用任何原型函数。但是,是的,最好完全设置它。注意:您遗漏了上面重要的
var
s,可能需要提及ES2015语法。(最后遗漏了一个
)@t.J.Crowder您非常小心。-“顺便说一句,您的继承看起来是错误的。”这没有错,因为他们没有使用任何原型函数。但是,是的,最好完全设置它。注意:您遗漏了上面重要的
var
s,可能需要提及ES2015语法。(最后遗漏了一个
。@t.J.Crowder您非常小心。:-)