是否可以从数组中向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'
Run Code Online (Sandbox Code Playgroud)
我最近写了很多Python,这是一个很好的模式,能够接受varargs并发送它们.例如
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'
Run Code Online (Sandbox Code Playgroud)
在JavaScript中是否可以发送一个数组作为参数数组?
因为我很懒,我创建的函数log基本上只是以下的缩写console.log:
function log() {
console.log.apply(console, arguments);
}
Run Code Online (Sandbox Code Playgroud)
每当我打电话给我时,我会在Google Chrome的开发者工具中看到记录的项目,右侧是记录项目的行号.但是,此行号始终相同,因为实际console.log调用位于代码中的某个特定位置(即我声明上述log函数的位置).
我也尝试过的只是:
var log = console.log;
Run Code Online (Sandbox Code Playgroud)
但这总是会引发错误:Illegal invocation.很奇怪,但我猜这不可能.
如何console.log使用开发者工具显示log调用的行号而不是实际console.log调用的位置来创建快捷方式?