📜  [].slice.call( (1)

📅  最后修改于: 2023-12-03 15:13:11.946000             🧑  作者: Mango

[].slice.call()

Introduction

[].slice.call() is a method in JavaScript that allows you to convert an array-like object (such as a NodeList or arguments object) into an actual array.

Syntax
var newArray = [].slice.call(arrayLikeObject, begin, end);
Parameters
  • arrayLikeObject: The array-like object you want to convert to an actual array.
  • begin (optional): The index at which to begin extraction.
  • end (optional): The index at which to end extraction.
Return value

[].slice.call() returns a new array containing the elements of the array-like object.

Examples
var fakeArray = { 0: "apple", 1: "banana", 2: "cherry", length: 3 };
var realArray = [].slice.call(fakeArray);

console.log(realArray); // ["apple", "banana", "cherry"]
function myFunction() {
  var args = [].slice.call(arguments);
  console.log(args); // [1, 2, 3]
}

myFunction(1, 2, 3);
Conclusion

[].slice.call() may seem like a simple method, but it can be extremely useful when working with array-like objects. Its ability to quickly and easily convert those objects into actual arrays makes it a valuable tool for JavaScript programmers.