Array.slice 和 Array().slice 的区别 [英] Difference between Array.slice and Array().slice

查看:30
本文介绍了Array.slice 和 Array().slice 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 John Resig 的优秀高级 javascript 教程,但我不完全理解是什么以下调用之间的区别:(请注意,'arguments' 是一个内置的 javascript 词,并不完全是一个数组,因此使用 Array.slice 进行黑客攻击,而不是简单地调用 arguments.slice)

<预><代码>>>>争论[3, 1, 2, 3]>>>Array.slice.call(参数)3,1,2,3 0=3 1=1 2=2 3=3>>>Array.slice.call( 参数, 1 )[]>>>Array().slice.call( 参数 )3,1,2,3 0=3 1=1 2=2 3=3>>>Array().slice.call( 参数, 1 )1,2,3 0=1 1=2 2=3

基本上我的误解归结为 Array.slice 和 Array().slice 之间的区别.这两者之间究竟有什么区别,为什么 Array.slice.call 没有按预期运行?(它返回除参数列表的第一个元素之外的所有元素).

解决方案

不太清楚.

看看当你调用 String.substring.call("foo", 1) 和 String().substring.call("foo", 2) 时会发生什么:

<预><代码>>>>String.substring.call("foo", 1)1">>>String().substring.call("foo", 1)哦"

Array.slice既没有正确引用附加到 Array 原型的 slice 函数,也没有正确引用附加到任何实例化 Array 实例(例如 Array() 或 [])的 slice 函数.

Array.slice 甚至根本不为 null 的事实是对象(/function/constructor)本身的错误实现.尝试在 IE 中运行等效代码,您将收到 Array.slice 为 null 的错误.

这就是 Array.slice 行为不正确的原因(String.substring 也没有).

证明(根据 slice() 的定义,以下是人们永远不会想到的……就像上面的 substring() 一样):

<预><代码>>>>Array.slice.call([1,2], [3,4])3,4

现在,如果您在实例化对象数组原型上正确调用 slice(),您将得到您所期望的:

<预><代码>>>>Array.prototype.slice.call([4,5], 1)[5]>>>Array().slice.call([4,5], 1)[5]

更多证据...

<预><代码>>>>Array.prototype.slice == Array().slice真的>>>Array.slice == 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)

>>> 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. What exactly is the difference between these two and why does not Array.slice.call behave as expected? (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)
"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 []).

The fact that Array.slice is even non-null at all is an incorrect implementation of the object (/function/constructor) itself. Try running the equivalent code in IE and you'll get an error that Array.slice is null.

This is why Array.slice does not behave correctly (nor does String.substring).

Proof (the following is something one should never expect based on the definition of slice()...just like substring() above):

>>> 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.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.slice 和 Array().slice 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆