在Chrome的控制台,为什么在这种情况下使用'apply'函数?
> $$
bound: function()
{
return document.querySelectorAll.apply(document, arguments)
}
这是为什么这样的代码? 与
有什么区别return document.querySelectorAll(arguments)
?
在Chrome的控制台,为什么在这种情况下使用'apply'函数?
> $$
bound: function()
{
return document.querySelectorAll.apply(document, arguments)
}
这是为什么这样的代码? 与
有什么区别return document.querySelectorAll(arguments)
?
arguments
是一个类似数组的对象。
调用document.querySelectorAll(arguments)
会将整个数组作为单个参数传递。
调用querySelectorAll.apply(document, arguments)
将传递数组中的每个项目作为单独的参数。
在这种特殊情况下,它不是很有用,因为querySelectorAll
只能带一个参数。
使用.apply()
确保供给到bound()
参数是被供应到querySelectorAll()
之前解开。
没有这一点,调用的效果:
bound('foo');
将调用
document.querySelectorAll(['foo']);