我试过以下但没有成功:
function a(args){
b(arguments);
}
function b(args){
// arguments are lost?
}
a(1,2,3);
Run Code Online (Sandbox Code Playgroud)
在函数a中,我可以使用arguments关键字来访问参数数组,在函数b中这些参数丢失了.有没有办法将参数传递给另一个javascript函数,就像我尝试做的那样?
我想使用数组作为参数来调用函数:
const x = ['p0', 'p1', 'p2'];
call_me(x[0], x[1], x[2]); // I don't like it
function call_me (param0, param1, param2 ) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
有路过的内容的一种更好的方式x进入call_me()?
是否可以将JavaScript中的数组转换为函数参数序列?例:
run({ "render": [ 10, 20, 200, 200 ] });
function run(calls) {
var app = .... // app is retrieved from storage
for (func in calls) {
// What should happen in the next line?
var args = ....(calls[func]);
app[func](args); // This is equivalent to app.render(10, 20, 200, 200);
}
}
Run Code Online (Sandbox Code Playgroud) 我想用params将函数推送到数组而不执行它们.这是我到目前为止所尝试的:
var load_helpers = require('../helpers/agentHelper/loadFunctions.js');
var load_functions = [];
load_functions.push(load_helpers.loadAgentListings(callback , agent_ids));
load_functions.push(load_helpers.loadAgentCount(callback , agent_data));
Run Code Online (Sandbox Code Playgroud)
但是通过这种方式,函数会在推送时执行.该问题提供了类似的例子,但没有参数.如何在此示例中包含参数?