javascript Array.prototype.slice.call(array, 0) 有什么用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5145032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 16:09:17  来源:igfitidea点击:

What's the use of Array.prototype.slice.call(array, 0)?

javascriptarrayssizzle

提问by pimvdb

I was just browsing Sizzle's source code and I came across this line of code:

我只是在浏览 Sizzle 的源代码,发现了这行代码:

array = Array.prototype.slice.call( array, 0 );

I looked up what the function is, but I came to the conclusion that it just returns all elements of the array starting from index 0, and puts the whole into the array, i.e. it doesn't really do anything at all.

我查了一下这个函数是什么,但我得出的结论是它只返回数组中从索引 0 开始的所有元素,并将整个元素放入数组中,即它根本没有做任何事情。

What is therefore the use of this line of code? What am I missing?

那么这行代码有什么用呢?我错过了什么?

Edit:It's line 863 from https://github.com/jquery/sizzle/blob/master/sizzle.js#L863.

编辑:这是来自https://github.com/jquery/sizzle/blob/master/sizzle.js#L863的第 863 行。

回答by Anurag

The DOM usually returns a NodeListfor most operations like getElementsByTagName.

NodeList对于大多数操作,DOM 通常会返回 a getElementsByTagName

Although a NodeListalmost feels like an array, it is not. It has a lengthproperty like an array does, and a method item(index)to access an object at the given index (also accessible with the [index]notation), but that's where the similarity ends.

虽然 aNodeList几乎感觉像一个数组,但它不是。它有一个length类似于数组的属性,以及一个item(index)访问给定索引处的对象的方法(也可以用[index]符号访问),但这就是相似性结束的地方。

So to be able to use the wonderful array methodswithout rewriting them all for a NodeList, the above line is useful.

所以为了能够使用美妙的数组方法而不用将它们全部重写为 a NodeList,上面的行很有用。

Another use of converting it to an array is to make the list static. NodeLists are usually live, meaning that if document changes occur, the NodeList object is automatically updated. That could cause problems, if a jQuery object returned to you kept changing right under your nose. Try the following snippetto test the liveness of NodeLists.

将其转换为数组的另一个用途是使列表成为静态。NodeLists 通常是活动的,这意味着如果发生文档更改,NodeList 对象会自动更新。如果返回给您的 jQuery 对象在您眼皮底下不断变化,这可能会导致问题。尝试以下代码片段来测试 NodeLists 的活跃度。

var p = document.getElementsByTagName('p');
console.log(p.length); // 2
document.body.appendChild(document.createElement('p'));
// length of p changes as document was modified
console.log(p.length); // 3

回答by James Sulak

What's happening here is that Sizzle is creating an actual array out of an array-like object. The array-like object doesn't necessarily have the slice() method, so the prototype method has to be called directly. makeArray()returns a copy of that array-like object that is an actual array, and can be used as such else where.

这里发生的事情是 Sizzle 正在从一个类似数组的对象中创建一个实际的数组。类数组对象不一定有slice()方法,所以必须直接调用prototype方法。 makeArray()返回一个类似数组的对象的副本,它是一个实际的数组,并且可以在其他地方使用。

See herefor more information about array-like objects.

有关类似数组的对象的更多信息,请参见此处

回答by harpo

As BoltClock says, it makes a (shallow) copy of an array. It can also be used to copy something that is almostan array, such as the argumentsbuiltin, which has a length and items but no Array in its prototype chain (and hence no slice method).

正如 BoltClock 所说,它制作了一个数组的(浅)副本。它还可以用于复制几乎是数组的东西,例如arguments内置函数,它有长度和项目,但在其原型链中没有数组(因此没有切片方法)。