Continuation series One: link, this is the main collation JS inside very common three function call,apply,bind the magical. (Apply and call are the same, just the difference between the parameters) (Some of the three below may be auxiliary, but as long as it's used, I'll sort it out here.)
1. Type detection
The last one has been mentioned, Object.prototype.toString.call (). Hesitate to say, there is not much to say.
2. The solution is equivalent to the ES6 ... Extension operators
An operator that facilitates the deconstruction of a string array is presented in ES6, which provides an easy mechanism to convert any data structure that deploys the Iterator interface to an array. As long as a data structure deploys the Iterator interface, you can use the extension operator on it to convert it to an array.
But in es5, we use Function.prototype.apply (NULL, array name),
function myFunction(x, y, z) { }var args = [0, 1, 2];myFunction.apply(null, args);
ES6:
function myFunction(x, y, z) { }var args = [0, 1, 2];myFunction(...args);
3. Converting pseudo-arrays
Definition of pseudo-array:
- Has the Length property
- Storing data by index
- Methods such as Push,pop that do not have arrays
- The arguments inside the function
- Document.forms,form.elements,select.options,document.getelementsbyname (), document.getElementsByTagName (), Childnodes/children such as collection (Htmlcollection,nodelist) and so on.
- Special-Written objects:
But in essence to distinguish the pseudo-array:
If the proto of an array directly or indirectly points to Array.prototye (using the common properties of the array), then the true array
If the proto of an array does not directly or indirectly point to Array.prototye, then it is a pseudo-array.
How to convert a pseudo-array to a group:
Use the method slice () in the prototype object of array to pass in Function.prototype.call.
Fantastic JS Code series (ii) Call,apply,bind use finishing