多重继承或访问对象外部的属性和方法

multi inherit or access the property and method outside the object

本文关键字:属性 方法 外部 访问 对象 多重继承      更新时间:2023-09-26
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
    }
};

是否可以使可以访问this.p this.function1

欢迎您的评论

如果您想在

a.func中访问它,则需要从内部保留对this t的引用。请尝试以下操作:

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