2014-01-05 146 views
2

一个功能需要整组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); 

回答

2

使用Function.apply

debugConsole : function(msg) { 
    if(this.isDebugOn()) { 
     console.log.apply(console, arguments); 
    } 
} 
相关问题