Javascript .slice(0) 在这里有什么意义?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5024085/
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-08-23 15:18:08  来源:igfitidea点击:

What's the point of .slice(0) here?

javascriptjquerytype-conversionslice

提问by mVChr

I was studying the jQuery source when I found this (v1.5 line 2295):

当我发现这个(v1.5 line 2295)时,我正在研究jQuery源代码:

namespace = new RegExp("(^|\.)" +
  jQuery.map( namespaces.slice(0).sort(), fcleanup ).join("\.(?:.*\.)?") + "(\.|$)");

My question is, why use slice(0)here?

我的问题是,为什么slice(0)在这里使用?

回答by Anon.

sort()modifies the array it's called on - and it isn't very nice to go around mutating stuff that other code might rely on.

sort()修改它被调用的数组 - 并且改变其他代码可能依赖的东西并不是很好。

slice()always returns a new array - the array returned by slice(0)is identical to the input, which basically means it's a cheap way to duplicate an array.

slice()总是返回一个新数组 - 返回的数组slice(0)与输入相同,这基本上意味着它是一种复制数组的廉价方法。

回答by ide

arr.slice(0)makes a copy of the original array by taking a slice from the element at index 0 to the last element.

arr.slice(0)通过从索引 0 处的元素到最后一个元素取一个切片来制作原始数组的副本。

It's also used to convert array-like objects into arrays. For example, a DOM NodeList(returned by several DOM methods like getElementsByTagName) is not an array, but it is an array-like object with a lengthfield and is indexable in JavaScript. To convert it to an array, one often uses:

它还用于将类数组对象转换为数组。例如,DOM NodeList(由多个 DOM 方法(如getElementsByTagName)返回)不是数组,而是具有length字段的类数组对象,并且在 JavaScript 中是可索引的。要将其转换为数组,通常使用:

var anchorArray = [].slice.call(document.getElementsByTagName('a'), 0)

回答by Abhi

slice(0)creates a new array identical to the original array. Many a times you want to preserve your original array and create a new one.

slice(0)创建一个与原始数组相同的新数组。很多时候您想保留原始数组并创建一个新数组。

If you use slice(1), it will create a different array starting from index position 1.

如果您使用 slice(1),它将从索引位置 1 开始创建一个不同的数组。

Similar things holds for strings as well.

类似的事情也适用于字符串。

回答by TNC

slice(0)allows you to return an array of the existing array you're referencing, in this case namespaces.

slice(0)允许您返回您正在引用的现有数组的数组,在本例中为命名空间。

回答by Luis Teijon

In addition to what @Anon said:

除了@Anon 所说的:

The slice()method selects the elements starting at the given start argument, and ends at, but does not include, the given end argument.

slice()方法选择从给定 start 参数开始的元素,并在给定的 end 参数处结束,但不包括。

Example1:

示例 1:

var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(1, 3);

The result of citrus will be:

柑橘的结果将是:

Orange,Lemon

Example2:

示例2:

var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(-3, -1);

The result of citrus will be:

柑橘的结果将是:

Lemon,Apple

Further information can be found here.

可在此处找到更多信息。