Home > Web Front-end > JS Tutorial > How to use Array.prototype.slice.apply_javascript skills

How to use Array.prototype.slice.apply_javascript skills

WBOY
Release: 2016-05-16 18:32:08
Original
1644 people have browsed it
Copy code The code is as follows:

function test(){ 
 //Convert the parameter to one Array
var args = Array.prototype.slice.apply(arguments);
alert(args);
}

arguments is an object unique to functions in JavaScript syntax Properties (Arguments object) are used to reference the actual parameters passed when calling the function. This object is much like an array. It has a length property and uses subscripts to obtain its elements, but it is not a real Array object. For more information about the Arguments object, please refer to "The Definitive Guide to JavaScript".
So, calling arguments.slice() directly will return an "Object doesn't support this property or method" error because arguments is not a real array. The significance of the above code calling Array.prototype.slice.apply(arguments) is that it can convert the parameter object of the function into a real array. We don’t know how the JavaScript script engine is implemented, but this method is indeed effective and has been tested on mainstream browsers. On the other hand, the relationship between the Arguments object and the Array object can also be inferred. If you often encounter situations when you need to convert arguments objects into Array for processing when writing JavaScript, this technique can help.
This technique comes from the famous Douglas Crockford. By extension, other Array prototype methods can also be applied to arguments, such as:
var arg0 = Array.prototype.shift.apply(arguments);
shift is also an instance method of Array, used to obtain and returns the first element of the array. Of course, although the above call is executable, it is completely redundant. It is not as simple and direct as calling arguments[0] directly. By extension, we can also apply this technique to many Array-like Collection objects, such as Array.prototype.slice.apply(document.getElementsByTagName('div')); However, unfortunately, IE does not support such a call. , Firefox and Opera both get correct results.
The $A() method added in Prototype 1.4 is also commonly used to convert arguments into arrays. Let’s look at its implementation:
Copy code The code is as follows:

var $A = Array.from = function(iterable) {
if(!iterable) return[];
if(iterable.toArray ) {
returniterable.toArray();
} else {
varresults = [];
for(vari=0; i returnresults;
} 
}

Prototype uses a for loop to construct a new array, in order to ensure maximum compatibility.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template