I have a customized (console.)log function
我有一个自定义(控制台。)日志功能
function clog(toConsole, toFile){
console.log(toConsole);
// other things
}
But now I can't pass it the string in 'value: %d', value
format
但是现在我无法将'value:%d'中的字符串传递给值格式
...unless I wrap it in an array
...除非我将它包装在一个数组中
clog(['to console: %d', value], 'other things'])
Is there a way to pass the elements of the array ['to console: %d', value]
to a function like: console.log('to console: %d', value)
有没有办法将数组['to console:%d',value]的元素传递给类似的函数:console.log('to console:%d',value)
1 个解决方案
#1
1
What you are looking for is Function.prototype.apply. It allows you to call a function with an array, which is filled in as the arguments. In this case
您正在寻找的是Function.prototype.apply。它允许您使用数组调用函数,该数组作为参数填充。在这种情况下
console.log.apply(console, ['to console: %d', value]);
will be equivalent to
将等同于
console.log('to console: %d', value);
#1
1
What you are looking for is Function.prototype.apply. It allows you to call a function with an array, which is filled in as the arguments. In this case
您正在寻找的是Function.prototype.apply。它允许您使用数组调用函数,该数组作为参数填充。在这种情况下
console.log.apply(console, ['to console: %d', value]);
will be equivalent to
将等同于
console.log('to console: %d', value);