簡體   English   中英

多繼承或訪問對象外部的屬性和方法

[英]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

    }

};

是否可以使can可以訪問this.p this.function1?

歡迎您發表評論

如果要在a.func訪問它,則需要在t內部保留this的引用。 請嘗試以下操作:

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();

    }

};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM