Understanding Array.prototype.slice.apply

Source: Internet
Author: User

1. First, we all understand in JS change this reference there are three ways, call (), apply (), bind ();

2. The Bind method is to change the function within this reference, simply no longer described;

3. For call () and apply () two methods, the difference is that the former is an indefinite length of the argument, the latter is an array, the bottom point is to apply the method of use;

principle :

We know that there is a class array object in JS, such as a {0:1,length:1} DOM object, or a arguments object;

Array is just a special object, the particularity of the array is now, its key is the order of the default integer (0,1,2 ... ), so the array does not have to specify a key name for each element, and each member of the object must specify the key name.

So arrays in JavaScript can also be seen as such objects.

   vararray = [123];   var obj = {      01,      12,      23,      length:3   }

Note that this length property is important, and with length you can iterate over the object like an array.

To implement a simple slice method,

function slice(start, end) {    vararray = [];    0;    end = end ? end : this.length;    for (var0; i < end; i++, j++) {        array[j] = this[i];    }    returnarray;}
Example: Array.prototype.slice.apply ({0:1,length:1});

Through apply, the slice method is directed to the object, which iterates through the generation of the new array object.


Note the point:

1. Note that when you use apply: Apply or call simply switches the invocation of this inside the function, but the method that is executed is still the method on the original object, even if you are Array.prototype.slice. The slice method is overridden on the call (obj) obj, and the slice method on the Array is still executed;

2. Because the Apply method (or call method) can also bind the object on which the function executes, the function is executed immediately, so the binding statement must be written in a function body. We recommend that you use the Bind method when you use the function to change this point.

3.

The Bind method returns a new function every time it is run, which creates some problems. For example, when listening to an event, you cannot write it as follows.

element.addEventListener(‘click‘,o.m.bind(o));

The above code indicates that the click event binds an anonymous function generated by the bind method. This will result in the inability to unbind, so the following code is invalid.

element.removeEventListener(‘click‘,o.m.bind(o));

The correct method is written as follows:

varlistener=o.m.bind(o);element.addEventListener(‘click‘,listener);//  ...element.removeEventListener(‘click‘,listener);

Understanding Array.prototype.slice.apply

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.