0
对不起,但我不知道如何提出这个问题......我将解释它。将方法附加到对象的解决方法,以便方法可以访问“this”关键字作为对象
我有这样的代码:
function jclass_getVar() {
return this.myvar;
}
function jclass() {
this.myvar = "ok";
this.funcAry = [];
}
jclass.prototype.getVar = jclass_getVar;
var obj = new jclass();
现在一切都很好。如果我访问obj.getVar()
它将返回ok
,但可以说我想创建一个dinamically功能数组。这是我的代码:
function myMethod1() {
return this.myvar;
}
function myMethod2() {
return this.myvar;
}
obj.funcAry.push(myMethod1);
obj.funcAry.push(myMethod2);
当我要调用的函数,我这样做:
for (var i in obj.funcAry) {
obj.tempmethod = obj.funcAry[i];
obj.tempmethod();
}
obj.tempmethod = undefined;
我不喜欢创建一个时间变量调用的方法是...有没有什么“好”的方法调用方法和方法“this”是对象?