Sachila Ranawaka
Sachila Ranawaka

Reputation: 41437

oop call parent function from child class

i know this question ask many time before but still i couldn't get my head around it. i'm using javascrit oop and i need to call the parent class function this.returnResult from the child class function 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();

Why parantCls.prototype.returnResult(this,'setting customField should be array not ' + typeof this.sCus); is not working. im also used call and apply like this

parantCls.prototype.returnResult.call(this,'setting customField should be array not ' + typeof this.sCus);

but still don't work. what is the problem

Upvotes: 0

Views: 208

Answers (1)

gzc
gzc

Reputation: 8629

If you inherit correctly using prototype chain.

This should work

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

By the way, your inheritance looks wrong. use this format.

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

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

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

Upvotes: 2

Related Questions