Pass an arbitrary number of parameters into a JavaScript function

前端 未结 3 1002
猫巷女王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:43

    Instead of calling the function, reference your function as an element in an associative array by name:

    var funcName = // parse your xml for function name
    var params = new Array();
    params[0] = 10.2; // from your parsed xml
    params[1] = 'some text'; // also from your parsed xml
    
    // functions are attached to some Object o:
    
    o[funcName](params); // equivalent to o.funcName(params);
    

    I wrote an example of the above here: http://jsbin.com/ewuqur/2/edit

提交回复
热议问题