简体   繁体   中英

Call function with array of parameters

Is there a way of doing this without altering the function?

function foo(bar1, bar2, bar3)
{
    return bar1 + bar2 + bar3;
}
var array = [1, 2, 3];
console.log(foo(array)); //6
console.log(foo.apply(null, array));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM