简体   繁体   中英

multi inherit or access the property and method outside the object

var ob = function(){

};
ob.prototype.func = function(){

};

var t = function(){
    this.p=0;
    this.function1(){

    }
    var a=new ob();
    a.func=function(){//overrides the func

         //hope to access this.p this.function1

    }

};

is it possible to make a can access this.p this.function1 ?

Your comment welcome

You need to keep a reference to this from inside t if you want to access it within a.func . Try the following:

var t = function(){
    var this_t = this; // Use this_t to access this.p and this.function1 inside a

    this.p=0;
    this.function1 = function(){

    }

    var a=new ob();
    a.func = function(){//overrides the func

        this_t.p = 1;
        this_t.function1();

    }

};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM