Fantastic JS Code series (ii) Call,apply,bind use finishing

Source: Internet
Author: User

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:

    1. Has the Length property
    2. Storing data by index
    3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.