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

发布于 2024-10-20 03:52:53 字数 402 浏览 5 评论 0 原文

我刚刚浏览 Sizzle 的源代码,遇到了这行代码:

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

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

那么这行代码有什么用呢?我缺少什么?

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

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

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只涨不跌 2024-10-27 03:52:53

对于大多数操作(例如 NodeList >getElementsByTagName。

虽然 NodeList 几乎感觉像一个数组,但事实并非如此。它有一个像数组一样的 length 属性,以及一个方法 item(index) 来访问给定索引处的对象(也可以使用 [index] 访问) 表示法),但这就是相似性的结束。

因此能够使用精彩的数组方法而无需重写它们对于 NodeList 来说,上面的行很有用。

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

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

The DOM usually returns a NodeList for most operations like getElementsByTagName.

Although a NodeList almost feels like an array, it is not. It has a length property 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.

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

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 snippet to test the liveness of 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
唯憾梦倾城 2024-10-27 03:52:53

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

请参阅此处< /a> 有关类似数组对象的更多信息。

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.

See here for more information about array-like objects.

素食主义者 2024-10-27 03:52:53

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文