是否可以从数组向 JavaScript 函数发送可变数量的参数?
var arr = ['a','b','c']
var func = function()
{
// debug
alert(arguments.length);
//
for(arg in arguments)
alert(arg);
}
func('a','b','c','d'); // prints 4 which is what I want, then 'a','b','c','d'
func(arr); // prints 1, then 'Array'
我最近写了很多 Python,能够接受可变参数并发送它们是一个很棒的模式。例如
def func(*args):
print len(args)
for i in args:
print i
func('a','b','c','d'); // prints 4 which is what I want, then 'a','b','c','d'
func(*arr) // prints 4 which is what I want, then 'a','b','c','d'
是否可以在 JavaScript 中发送一个数组作为参数数组?