例如,一个接受变量 # of args 的函数需要将整个参数集传递到另一个也接受变量 # of args 的函数(以下内容不起作用):
debugConsole : function(msg) {
if(this.isDebugOn()) {
console.log(arguments); // does not work, prints "[object Arguments]"
}
}
我需要这个,以便 console.log() 的字符串替换功能起作用,即
var myObject = {name: "John Doe", id: 1234};
// should print "obj = [object Object]"
this.debugConsole("myObject = %o", myObject);
// should print "name: John Doe, ID: 1234"
this.debugConsole("name: %s, ID: %i", myObject.name, myObject.id);
最佳答案
使用Function.apply :
debugConsole : function(msg) {
if(this.isDebugOn()) {
console.log.apply(console, arguments);
}
}
关于javascript - 如何将所有可变数量的参数传递给接受可变数量参数的另一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20940658/