Javascript code:
function doSomething(v1,v2){ //blah; }
function SomeClass(callbackFunction,callbackFuncParameters(*Array*))={
this.callback = callbackFunction;
this.method = function(){
this.callback(parameters[0],parameters[1]) // *.*
}
}
var obj = new SomeClass( doSomething, Array('v1text','v2text') );
The problem is if I change function doSomething to
问题是如果我将函数doSomething更改为
function doSomething(v1,v2,v3){ //blah; }
I have to change the corresponding line (marked as //*.*
) in SomeClass
to
我必须在SomeClass中更改相应的行(标记为//*.*)
this.callback(parameters[0],parameters[1],parameters[2]);
What can be done to avoid the (*.*)
line to be changed no matter how the number of 'doSomething' function's parameters is changed?
无论“doSomething”函数的参数数量如何变化,可以采取哪些措施来避免改变(*。*)行?
Thanks a lot!
非常感谢!
1 个解决方案
#1
You probably want to use the apply method
您可能想要使用apply方法
this.callback.apply(this, parameters);
The first parameter to apply indicates the value of "this" within the callback and can be set to any value.
要应用的第一个参数表示回调中“this”的值,可以设置为任何值。
#1
You probably want to use the apply method
您可能想要使用apply方法
this.callback.apply(this, parameters);
The first parameter to apply indicates the value of "this" within the callback and can be set to any value.
要应用的第一个参数表示回调中“this”的值,可以设置为任何值。