为什么console.log()polyfills不使用Function.apply()? [英] Why do console.log() polyfills not use Function.apply()?
问题描述
我一直在看一些流行的 console.log()
wrapper / polyfills:
我注意到他们都接受多个参数
,但他们所有这些都是这样的:
console.log(arguments);
这样做会导致输出(在Chrome中):
而至少在Chrome或Firefox这样的现代浏览器中, console.log()
也接受多个参数,这样就可以产生(IMHO)优越的输出:
console.log这个(在Chrome中)的输出是这样的(在Chrome中):
>
有什么特别的原因我应该避免使用 console.log.apply()
有多个参数?或者这只是一个味道或保存字节的问题?
解决方案我个人建议您只使用 .apply()
当你必须: .apply()
是传递数组作为函数的参数的唯一方法。如果您不需要传递数组,那么只需使用 console.log()
。它不是冗长的,它是直接调用。
I've been looking at some of the popular console.log()
wrappers/polyfills:
I notice that all of them accept multiple arguments
, but they all do something like this:
console.log(arguments);
Which results in output like this (in Chrome):
Whereas, at least in a modern browser like Chrome or Firefox, console.log()
also accepts multiple arguments, so that this would produce (IMHO) superior output:
console.log.apply(console, arguments)
Which results in output like this (in Chrome):
Is there any particular reason why I should avoid using console.log.apply()
with multiple arguments? Or this this just a matter of taste or saving bytes?
解决方案 I would personally suggest that you only use .apply()
when you have to: .apply()
is the only way to pass an array as the arguments of a function. If you don't need to pass an array, then just use console.log()
. It is less verbose and it is a direct invocation.
这篇关于为什么console.log()polyfills不使用Function.apply()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!