Looking at this tutorial I saw the following code suggestion in one comment:
看一下这个教程,我在一条评论中看到了以下代码建议:
init:function(callback){
var that =this ;
return $http.jsonp(this.url).success(
function(data){
that.availableGenres = that.getGenres(data);
that.results = that.getResults(data);
if(callback)callback.apply(null,[].slice.call(arguments))
}
)
}
But this line callback.apply(null,[].slice.call(arguments))
looks weird to me.
但是这行callback.apply(null,[]。slice.call(arguments))看起来很奇怪。
Why not just: callback.apply(null, arguments)
? Because I don't like when I don't understand the point of something I played around with this Fiddle to understand the need of slice
function in there. But it gives me the same result and I still do not get it.
为什么不呢:callback.apply(null,arguments)?因为我不喜欢当我不理解我用这个小提琴玩的东西的意义,以了解切片功能的需要。但它给了我相同的结果,我仍然没有得到它。
Anyone knows why slice
is needed?
谁知道为什么需要切片?
2 个解决方案
#1
28
You don't really need it, passing arguments
to apply
directly is fine.
你真的不需要它,传递参数直接申请是好的。
It is specified to be allowed in EcmaScript 3, and EcmaScript 5 even allows any array-like objects not only arrays and arguments objects. Maybe it was needed to be backwards-compatible with even earlier or buggy JS implementations. Also, (as you see from the other answers), some people don't know about this fact and think they would need to pass actual arrays - so they are unnecessary careful.
它被指定在EcmaScript 3中被允许,并且EcmaScript 5甚至允许任何类似数组的对象,而不仅仅是数组和参数对象。也许它需要与早期或有缺陷的JS实现向后兼容。另外,(正如你从其他答案中看到的),有些人不知道这个事实,并认为他们需要传递实际的数组 - 所以他们是不必要的小心。
#2
2
slice is needed because function.apply requires an array, arguments is not a real array but can be converted to one using slice.
因为function.apply需要一个数组,所以需要切片,参数不是真正的数组,但可以使用切片转换为一个数组。
The arguments object is not an Array. It is similar to an Array, but does not have any Array properties except length. For example, it does not have the pop method. However it can be converted to a real Array:
arguments对象不是Array。它类似于Array,但除了length之外没有任何Array属性。例如,它没有pop方法。但是它可以转换为真正的数组:
var args = Array.prototype.slice.call(arguments);
this is one of the ugly parts of javascript
这是javascript的丑陋部分之一
#1
28
You don't really need it, passing arguments
to apply
directly is fine.
你真的不需要它,传递参数直接申请是好的。
It is specified to be allowed in EcmaScript 3, and EcmaScript 5 even allows any array-like objects not only arrays and arguments objects. Maybe it was needed to be backwards-compatible with even earlier or buggy JS implementations. Also, (as you see from the other answers), some people don't know about this fact and think they would need to pass actual arrays - so they are unnecessary careful.
它被指定在EcmaScript 3中被允许,并且EcmaScript 5甚至允许任何类似数组的对象,而不仅仅是数组和参数对象。也许它需要与早期或有缺陷的JS实现向后兼容。另外,(正如你从其他答案中看到的),有些人不知道这个事实,并认为他们需要传递实际的数组 - 所以他们是不必要的小心。
#2
2
slice is needed because function.apply requires an array, arguments is not a real array but can be converted to one using slice.
因为function.apply需要一个数组,所以需要切片,参数不是真正的数组,但可以使用切片转换为一个数组。
The arguments object is not an Array. It is similar to an Array, but does not have any Array properties except length. For example, it does not have the pop method. However it can be converted to a real Array:
arguments对象不是Array。它类似于Array,但除了length之外没有任何Array属性。例如,它没有pop方法。但是它可以转换为真正的数组:
var args = Array.prototype.slice.call(arguments);
this is one of the ugly parts of javascript
这是javascript的丑陋部分之一