简体   繁体   English

Array.slice和Array()。slice之间的区别

[英]Difference between Array.slice and Array().slice

I am going through John Resig's excellent Advanced javascript tutorial and I do not thoroughly understand what's the difference between the following calls: (please note that 'arguments' is a builtin javascript word and is not exactly an array hence the hacking with the Array.slice instead of simply calling arguments.slice) 我正在阅读John Resig的优秀高级JavaScript教程 ,我不完全理解以下调用之间的区别:(请注意'参数'是内置的javascript单词,并不完全是一个数组,因此使用Array.slice进行黑客攻击而不是简单地调用arguments.slice)

>>> arguments  
[3, 1, 2, 3]  
>>> Array.slice.call( arguments )  
3,1,2,3 0=3 1=1 2=2 3=3  
>>> Array.slice.call( arguments, 1 )  
[]
>>> Array().slice.call( arguments )  
3,1,2,3 0=3 1=1 2=2 3=3  
>>> Array().slice.call( arguments, 1 )  
1,2,3 0=1 1=2 2=3  

Basically my misunderstanding boils down to the difference between Array.slice and Array().slice. 基本上我的误解归结为Array.slice和Array()。slice之间的区别。 What exactly is the difference between these two and why does not Array.slice.call behave as expected? 这两者之间究竟有什么区别以及为什么Array.slice.call没有按预期运行? (which is giving back all but the first element of the arguments list). (它返回除了参数列表的第一个元素之外的所有元素)。

Not quite. 不完全的。

Watch what happens when you call String.substring.call("foo", 1) and String().substring.call("foo", 2): 观察调用String.substring.call(“foo”,1)和String()时会发生什么.substring.call(“foo”,2):

>>> String.substring.call("foo", 1)
"1"

>>> String().substring.call("foo", 1)
"oo"

Array.slice is neither properly referencing the slice function attached to the Array prototype nor the slice function attached to any instantiated Array instance (such as Array() or []). Array.slice 既没有正确引用附加到Array原型的slice函数,也没有附加到任何实例化Array实例(例如Array()或[])的slice函数。

The fact that Array.slice is even non-null at all is an incorrect implementation of the object (/function/constructor) itself. Array.slice甚至根本不是null的事实是对象(/ function / constructor)本身的不正确实现。 Try running the equivalent code in IE and you'll get an error that Array.slice is null . 尝试在IE中运行等效代码,您将收到Array.slice为null的错误

This is why Array.slice does not behave correctly (nor does String.substring). 这就是Array.slice行为不正确的原因(String.substring也没有)。

Proof (the following is something one should never expect based on the definition of slice()...just like substring() above): 证明(以下是基于slice()的定义永远不会期望的......就像上面的substring()一样):

>>> Array.slice.call([1,2], [3,4])
3,4

Now, if you properly call slice() on either an instantiated object or the Array prototype, you'll get what you expect: 现在,如果您在实例化对象 Array原型上正确调用slice(),您将得到您期望的结果:

>>> Array.prototype.slice.call([4,5], 1)
[5]
>>> Array().slice.call([4,5], 1)
[5]

More proof... 更多证据......

>>> Array.prototype.slice == Array().slice
true
>>> Array.slice == Array().slice
false

Array is just a function, albeit a special one (used to initialize arrays). 数组只是一个函数,虽然是一个特殊的函数(用于初始化数组)。 Array.slice is a reference to the slice() function in the Array prototype. Array.slice是对Array原型中slice()函数的引用。 It can only be called on an array object and not on the Constructor (ie Array) itself. 它只能在数组对象上调用,而不能在构造函数(即数组)本身上调用。 Array seems to behave specially though, as Array() returns an empty array. 数组似乎表现得特别,因为Array()返回一个空数组。 This doesn't seem to work for non-builtin Constructor functions (there you have to use new). 这对于非内置构造函数似乎不起作用(你必须使用new)。 So 所以

Array().slice.call

is the same as 是相同的

[].slice.call

How is any call to slice.call() working in the examples provided since a context parameter is not being supplied? 由于没有提供上下文参数,所以对提供的示例中的slice.call()的调用是如何工作的? Does slice implement it's own call method, thus overriding JavaScript's call method? slice是否实现了它自己的调用方法,从而覆盖了JavaScript的调用方法? The call and apply methods take as the first parameter an object to specify the context (this) object to apply to the invocation. call和apply方法将对象的第一个参数作为指定要应用于调用的上下文(this)对象的参数。

I believe Array is the type and Array() is the constructor function. 我相信Array是类型而Array()是构造函数。

Messing around in FireBug : FireBug乱搞

>>> Array === Array()
false

>>> Array.constructor
Function()

>>> Array().constructor
Array()

Well, 好,

Looking at http://www.devguru.com/Technologies/ecmascript/quickref/slice.html 查看http://www.devguru.com/Technologies/ecmascript/quickref/slice.html

Array().slice is a function (constructor)in the array class, It cant be used as a data member. Array()。slice是数组类中的函数(构造函数),它不能用作数据成员。 If you didn't want to use the '()' you would need to call it on the array. 如果你不想使用'()',你需要在数组上调用它。 ie - arguments.slice(1) ie - arguments.slice(1)

My guess is that Array is a prototype while Array() is an actual array object. 我的猜测是Array是一个原型,而Array()是一个实际的数组对象。 Depending on the JavaScript interpretation, directly calling the prototype method of a builtin object type might work or it might not. 根据JavaScript解释,直接调用内置对象类型的原型方法可能有效,也可能不成功。 I don't believe the spec says it has to work, just that calling it on a instantiated object works. 我不相信规范说它必须工作,只是在实例化的对象上调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM