Pass an arbitrary number of parameters into a JavaScript function

前端 未结 3 968
猫巷女王i
猫巷女王i 2021-01-13 04:02

My JavaScript code is barely an Ajax request that expects XML to be returned from back-end. The back-end can return execute_callback as one of XML tags like thi

3条回答
  •  情话喂你
    2021-01-13 04:45

    You could refer to Function.apply. Assuming the callback functions are declared in global object (window in browser).

    var callback_function = window[function_name];
    if (callback_function) { // prevent from calling undefined functions
        callback_function.apply(window, params);  // params is an array holding all parameters
    }
    

提交回复
热议问题