Array.prototype.sort.apply( someArray, args ) vs someArray.sort.apply( someArray, args ) [英] Array.prototype.sort.apply( someArray, args ) vs someArray.sort.apply( someArray, args )
问题描述
sorted_copy
函数的以下两个实现之间是否存在实质性差异(注意:只有最后几行不同):
Is there a substantial difference between the following two implementations of a sorted_copy
function (NOTE: only their last lines differ):
/* version 1 */
function sorted_copy ( array ) {
var extra_args = Array.prototype.slice.call( arguments, 1 );
var copy = array.slice();
return Array.prototype.sort.apply( copy, extra_args );
}
/* version 2 */
function sorted_copy ( array ) {
var extra_args = Array.prototype.slice.call( arguments, 1 );
var copy = array.slice();
return copy.sort.apply( copy, extra_args );
}
<小时>
更一般地,假设我们手头有一个变量someInstance
1,它的值确实是SomeType
2的实例,并且 args
是 Array
的一些合适的实例,那么什么时候有理由更喜欢下面的一个?
More generally, assuming that we have a variable someInstance
on hand1, whose value is indeed an instance of SomeType
2, and that args
is some suitable instance of Array
, then when is there a reason to prefer one over the other of the following?
SomeType.prototype.someMethod.apply( someInstance, args );
someInstance.someMethod.apply( someInstance, args );
同样,如果 arg1
, arg2
, ... 是一些值序列,什么时候有理由更喜欢下面的一个或另一个?
Similarly, if arg1
, arg2
, ... is some sequence of values, when is there reason to prefer one or the other of the following?
SomeType.prototype.someMethod.bind( someInstance, arg1, arg2, ... );
someInstance.someMethod.bind( someInstance, arg1, arg2, ... );
<小时>
1 假设所有其他条件都相同,我认为 SomeType.prototype.someMethod...code> 形式的优点是它们可能不需要创建一个变量只是为了获得它的
someMethod
.例如,在sorted_copy
的第1 版中,可以不用定义copy
变量(即函数体的第二行),而只需返回Array.prototype.sort.apply( array.slice(), extra_args )
.上面关于 someInstance
变量的存在应该被视为给定的规定只是为了利用 SomeType.prototype.someMethod...
形式的这个小可能的好处出于本问题的考虑.
1 Assuming that all other things are equal, I suppose that a possible benefit of the SomeType.prototype.someMethod...
forms is that they may obviate the need to create a variable only to get a hold of its someMethod
. For example, in version 1 of sorted_copy
, one could dispense with defining the copy
variable (i.e. the function body's second line), and just return Array.prototype.sort.apply( array.slice(), extra_args )
. The stipulation above that the existence of the someInstance
variable should be taken as given is there only to take this small possible benefit of the SomeType.prototype.someMethod...
forms out of consideration for the purposes of this question.
2 当然不用说,someInstance.someMethod
方法就是它继承的方法,它是SomeType 的一个实例
,否则这个帖子的问题就变得微不足道了.
2 Of course, it goes without saying that the method someInstance.someMethod
is the one it inherits by virtue of being an instance of SomeType
, otherwise this post's questions become trivial.
推荐答案
Array.prototype.sort.
和 someArray.sort.
(注意尾随点)同样的东西,所以没有区别.Array.prototype.sort()
和 someArray.sort()
是不同的,但这是另一个问题.
Array.prototype.sort.
and someArray.sort.
(note the trailing dots) is exactly the same thing, so there's no difference. Array.prototype.sort()
and someArray.sort()
are different, but that's another question.
出于性能原因,最好使用 Array.prototype
,因为您可以将其缓存在您的命名空间中,例如参见下划线来源.
For performance reasons, it might be better to use Array.prototype
, since you can cache it in your namespace, see e.g. underscore sources.
这篇关于Array.prototype.sort.apply( someArray, args ) vs someArray.sort.apply( someArray, args )的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!