Array.prototype.slice.call(arguments)
can convert objects with the length attribute into an array, except for the node collection under IE (because the dom object under ie is based on com The form of the object is realized, the js object and the com object cannot be converted)
such as:
< br>var a={length:2, 0:'first',1:'second'};//class array, with length attribute, the length is 2, the first 0 is first, the first 1 is the second console.log(Array.prototype.slice.call(a ,0));// ["first ", "second"], call the slice of the array(0); var a={length:2,0:'first',< span class="hljs-number">1:'second'}; console.log(Array.prototype.slice.call(a, 1));//["second" ], call the slice of the array(1); var a={0: 'first',1:'second'};//Removelength property, returns an empty array console.log(Array.prototype.slice .call(a,0));/ /[] function test(){ console.log(Array.prototype.slice.call(arguments,0));//[< span class="hljs-string">"a", "b", "c"], slice(0) console.log(Array.prototype.slice.call(arguments,1));< span class="hljs-regexp">//["b", "c"],slice(1)} test("a","b","c");< /span>< /span>< /span>< /span>< /span>
Addition:
Method for converting the actual parameters of a function into an array
Method 1: var args = Array.prototype.slice.call(arguments);
Method 2: var args = [].slice.call(arguments, 0);< /code>
Method 3:
var args = []; for (var i = 1; i <arguments.length; i++) { args.push(arguments[i]); }
Finally, attach a general function that turns into an array
var toArray = function( s){ try{ return Array.prototype.slice.call(s);} catch( e){ var arr = []; for (var i = 0,len = s. length; i //arr.push(s[i]); arr[i] = s[i]; < span class="hljs-comment">//It is said that this is faster than push} return arr; } } span>