Pass an arbitrary number of parameters into a JavaScript function

前端 未结 3 998
猫巷女王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 05:01

    You can just pass them all into your function:

    function someFunction(){
        for(i = 0; i < arguments.length; i++)
            alert(arguments[i]);
    }
    

    Javascript functions have an arguments array-like object, and it is syntactically correct to call a javascript function with any number of arguments.

    someFunction(1, 2, 'test', ['test', 'array'], 5, 0);
    

    is a valid call to that function.

提交回复
热议问题