I have this json, and when i get this json i need to run the function which comes at callback object.
我有这个json,当我得到这个json时,我需要运行回调对象的函数。
{
formId: 'snn_service_item_form',
item_id: '1',
item_title: 'some item',
item_description: '',
item_duration: '10',
item_price: '120',
item_level_1 : 1,
item_level_2 : 0,
item_level_3 : 1,
item_type: 'p',
callback : {
callbackName : 'getServices',
callbackParams : {
_param1 : 1,
_param2 : 2
}
}
}
so according to this i need to run this:
所以根据这个我需要运行这个:
getServices(1,2);
i can do that with eval function like:
我可以用eval函数做到这一点,如:
eval(json.callback.callbackName+'(\''+ json.callback.callbackNParams._param1 +'\',\''+ json.callback.callbackNParams._param2 +'\')');
i can automate this by putting it into a for in and writing parameters to a string, but i dont think this is the best way to go.
我可以通过将其放入for in并将参数写入字符串来自动执行此操作,但我不认为这是最好的方法。
is there a way to assign function name from a var and giving its parameters as an object, in my case like:
有没有办法从var分配函数名称并将其参数作为对象,在我的情况下,如:
json.callback.callbackName(json.callback.callbackParams);
i know this is not the way to do it but it is what i want to learn.
我知道这不是这样做的方式,但这是我想要学习的。
Thanks, Sinan.
谢谢,思南。
2 个解决方案
#1
11
Depends on where the function to call is defined (global scope or a local scope).
取决于定义要调用的函数的位置(全局范围或本地范围)。
If global, you don't need eval
(and it's safer to avoid it), you just reference the function through the global window
object:
如果是全局的,则不需要eval(并且避免它更安全),您只需通过全局窗口对象引用该函数:
var args = [];
for(var p in json.callback.callbackParams) {
args.push(json.callback.callbackParams[p]);
}
window[json.callback.callbackName].apply(null, args)
See the apply()
function used above.
请参阅上面使用的apply()函数。
If it's in a local scope, then you need the eval
(how you have it is fine).
如果它在本地范围内,那么你需要eval(如何使用它)。
#2
2
Don't use eval. You can get a reference to a named global variable or function from the window
object:
不要使用eval。您可以从window对象获取对命名全局变量或函数的引用:
var callbackfunction= window[json.callback.callbackName];
And trying to serialise your values to a string just to have them parsed back to JavaScript unreliably is silly. Call the function explicitly:
并尝试将您的值序列化为字符串只是为了让它们不可靠地解析回JavaScript是愚蠢的。明确调用函数:
callbackfunction.call(window, json.callback.callbackParams.param1, json.callback.callbackParams.param2);
(window
here is a dummy value for this
for when you're not using object methods.)
(当你不使用对象方法时,这里的窗口是一个虚拟值。)
Better for automating it to accept any number of parameters would be to turn callbackParams into a plain Array:
更好的自动化接受任意数量的参数将把callbackParams变成一个普通的数组:
callbackParams: [1, 2]
and then use apply
to call the function:
然后使用apply来调用函数:
callbackfunction.apply(window, json.callback.callbackParams);
#1
11
Depends on where the function to call is defined (global scope or a local scope).
取决于定义要调用的函数的位置(全局范围或本地范围)。
If global, you don't need eval
(and it's safer to avoid it), you just reference the function through the global window
object:
如果是全局的,则不需要eval(并且避免它更安全),您只需通过全局窗口对象引用该函数:
var args = [];
for(var p in json.callback.callbackParams) {
args.push(json.callback.callbackParams[p]);
}
window[json.callback.callbackName].apply(null, args)
See the apply()
function used above.
请参阅上面使用的apply()函数。
If it's in a local scope, then you need the eval
(how you have it is fine).
如果它在本地范围内,那么你需要eval(如何使用它)。
#2
2
Don't use eval. You can get a reference to a named global variable or function from the window
object:
不要使用eval。您可以从window对象获取对命名全局变量或函数的引用:
var callbackfunction= window[json.callback.callbackName];
And trying to serialise your values to a string just to have them parsed back to JavaScript unreliably is silly. Call the function explicitly:
并尝试将您的值序列化为字符串只是为了让它们不可靠地解析回JavaScript是愚蠢的。明确调用函数:
callbackfunction.call(window, json.callback.callbackParams.param1, json.callback.callbackParams.param2);
(window
here is a dummy value for this
for when you're not using object methods.)
(当你不使用对象方法时,这里的窗口是一个虚拟值。)
Better for automating it to accept any number of parameters would be to turn callbackParams into a plain Array:
更好的自动化接受任意数量的参数将把callbackParams变成一个普通的数组:
callbackParams: [1, 2]
and then use apply
to call the function:
然后使用apply来调用函数:
callbackfunction.apply(window, json.callback.callbackParams);