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

Asked
Active3 hr before
Viewed126 times

7 Answers

array
90%

Meta Stack Overflow , Stack Overflow help chat ,Stack Overflow en español,Stack Overflow на русском

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
88%

If start is undefined, slice starts from the index 0. , Function.arguments, If start is greater than the index range of the sequence, an empty array is returned. , If end is omitted, slice extracts through the end of the sequence (arr.length).

slice()
slice(start)
slice(start, end)
load more v
72%

I know it is used to make arguments a real array, but I don't understand what happens when using Array.prototype.slice.call(arguments), javascript - Since "parameter" is not a real array, why Array.prototype.slice . call (arguments) works, but Array.prototype.slice . call (someobject) doesn't work? , javascript - Array.prototype.slice What is the use of. Call (array, 0)?

I know it is used to make arguments a real array, but I don't understand what happens when using Array.prototype.slice.call(arguments)

Array.prototype.slice.call(arguments)
65%

Array.generate = function (length, fill) { var arr, l; length = length >>> 0; if (arguments.length < 2) { throw new TypeError("Cannot generarte an array without provided fill."); } arr = slice.call(arguments, 1, 1 + length); while ((l = arr.length) < length) { arr = arr.concat(arr.slice(0, length - l)); } return arr; };,Array.generate = function (length, fill) { var arr, l; length = length >>> 0; if (arguments.length < 2) { throw new TypeError("Cannot generarte an array without provided fill."); } arr = slice.call(arguments, 1, 1 + length); while ((l = arr.length) < length) { arr = arr.concat(arr.slice(0, length - l)); } return arr; };,Function (not method) that returns generated array may make more sense, currently I'm using something like that:,Creating a 10000 item array by repeating a 1000 item array 10 times, the difference is negligible when compared the implementation that I wrote, not the one given above

Implementation:

Array.prototype.repeat = function(times) {
   var result = [];
   var len = this.length;
   var resultLen = len * times;
   for (var i = 0; i < resultLen; i++) {
      result.push(this[i % len]);
   }
   return result;
}

In use:

$[1, 2, 3].repeat(3)[1, 2, 3, 1, 2, 3, 1, 2, 3]
load more v
75%

The classList attribute returns the class name of the element as a DOMTokenList object.,This attribute is used to add, remove and switch CSS classes in the element.,Return value: a DOMTokenList, a list of class names containing elements,The classList attribute is read-only, but you can modify it using the add() and remove() methods.

document.getElementById("myDIV").classList.add("mystyle");
load more v
40%

The Array.prototype object provides the slice() method that allows you to extract subset elements of an array and add them to the new array. In this tutorial, we will show you the practical uses of the JavaScript array slice() method.,In this tutorial, you have learned how to use the JavaScript array slice() method to copy an entire or a subset of the array and convert an array-like object into an array.,In this example, we instantiated an empty array [] and indirectly accessed the slice() method of the Array.prototype method through the empty array. The effect is the same as the one that uses the Array.prototype directly.,The typical use of the slice() method is to copy a portion of an array without modifying the source array. Here is an example:

The slice() method accepts two optional parameters as follows:

.wp - block - code {
      border: 0;
      padding: 0;
   }

   .wp - block - code > div {
      overflow: auto;
   }

   .shcb - language {
      border: 0;
      clip: rect(1 px, 1 px, 1 px, 1 px); -
      webkit - clip - path: inset(50 % );
      clip - path: inset(50 % );
      height: 1 px;
      margin: -1 px;
      overflow: hidden;
      padding: 0;
      position: absolute;
      width: 1 px;
      word - wrap: normal;
      word - break: normal;
   }

   .hljs {
      box - sizing: border - box;
   }

   .hljs.shcb - code - table {
      display: table;
      width: 100 % ;
   }

   .hljs.shcb - code - table > .shcb - loc {
      color: inherit;
      display: table - row;
      width: 100 % ;
   }

   .hljs.shcb - code - table.shcb - loc > span {
      display: table - cell;
   }

   .wp - block - code code.hljs: not(.shcb - wrap - lines) {
      white - space: pre;
   }

   .wp - block - code code.hljs.shcb - wrap - lines {
      white - space: pre - wrap;
   }

   .hljs.shcb - line - numbers {
      border - spacing: 0;
      counter - reset: line;
   }

   .hljs.shcb - line - numbers > .shcb - loc {
      counter - increment: line;
   }

   .hljs.shcb - line - numbers.shcb - loc > span {
      padding - left: 0.75 em;
   }

   .hljs.shcb - line - numbers.shcb - loc::before {
      border - right: 1 px solid #ddd;
      content: counter(line);
      display: table - cell;
      padding: 0 0.75 em;
      text - align: right; -
      webkit - user - select: none; -
      moz - user - select: none; -
      ms - user - select: none;
      user - select: none;
      white - space: nowrap;
      width: 1 % ;
   }
slice(start, stop);
load more v
22%

The slice method returns a specified slice of the source array.,When it returns -1, it means it can’t find the specified member in the array.,There are also a number of functional methods for looping over arrays in JS. These methods exist on the Array.prototype, and include:,This is an ultimate article on arrays in JavaScript. We’ll go in depth of this data structure in JS, and understand a number of different use cases, tips, tricks, and techniques.

var arr = ['one', 'two', 'three', 'four'];
delete arr[0];
console.log(arr); // returns: [undefined, 'two', 'three', 'four']
load more v

Other "array-undefined" queries related to "What's the use of Array.prototype.slice.call(array, 0)?"