Introduction to javascript Array. prototype. slice. call_javascript skills-js tutorial

Source: Internet
Author: User
Tags javascript array
It is found that most people use Array. prototype. slice. call (argments, 0) and never understand what this sentence is. I reviewed the slice () method yesterday, and then referred to Function. call (thisArg [, arg1 [, arg2 [,...]), I still don't know (my mind is slow: | ). Writing front

In js, we often see Array. prototype. slice. call (arguments, 0), of course, the role of this method may be understood, that is, to convert the class array object into a real array. I will explain my understanding about this method.

The slice () method and call () method are involved here, so let's talk about these two methods first.

Slice () method

The slice method is available for arrays and strings. The function of this method is to intercept a piece of data. It receives two parameters. The first parameter is the location index to be intercepted, and the second parameter is optional, indicating the end position to be intercepted, but not the end position. In an array, the return value of this method is an array composed of truncated elements. In a string, the return value of this method is a string consisting of truncated strings.

This method can also be used to pass in a negative value. When the parameter is a negative value, the positive value obtained by adding the parameter and the length of the array or string is used as the actual parameter.

As follows:

[1,2,3,4,5,6].slice(2,4);[1,2,3,4,5,6].slice(-4,-2);

Returns an array of [3, 4] values.

'everything'.slice(2,4);'everything'.slice(-4,-2);

The return values are 'er' and 'hi', respectively.

If a parameter is input, all elements from the start position to the end position are output. No more examples.

Other similar methods of strings

In the string, there are two methods of the slice () method type:

Substring () and substr () methods.

The substring () method returns the string from the start position to the end position. substr () receives two parameters. The first parameter indicates the start position, the second parameter indicates the number of characters to be truncated, which is slightly different from the first two methods.

When the input method parameter is negative, the three methods are slightly different.

When the input method parameter is negative:

As mentioned above, slice () is a negative number plus the length of the string to get the corresponding positive value;

The parameters of the substring () method are set to zero;

The first parameter of the substr () method is a negative value and a positive value of string length. The second parameter is set to zero.

Call () and apply () Methods

The call () and apply () methods are mainly used to expand the function scope.

The call () and apply () methods receive two parameters:

Apply (): the first parameter is the scope, and the second parameter is the parameter array. The second parameter can be an array instance or an arguments object.

The call () method also receives two parameters, except that they are different from the parameter passing method of apply (): the parameters of the passing function must be written one by one.

Since this is not the focus, I will not repeat it here.

Array.prototype.slice.call(arguments,0)

In Array. prototype. slice. in call (arguments, 0), Array. prototype. slice calls the prototype method of Array. For a positive Array, there is a slice () method. However, for a class Array object such as arguments or its own, although there are several attributes such as length, but there is no slice () method, so for this Array object, you must use the prototype method to use the slice () method, that is, Array. prototype. slice (if you customize the slice () method in the Custom class array object, you can directly call it ).

Therefore, Array. prototype. slice. the meaning of call (arguments, 0) can be understood as follows: for the arguments class Array, we call Array. prototype. slice prototype method, and use the call () method to restrict the scope to arguments. Here, Array. prototype can be understood as arguments. The same parameter 0 is the first parameter of the slice () method, that is, the start position index. In this way, the arguments class array is converted into a real array.

Of course, you can also use traversal to convert arguments into an array, so that the Code will naturally be more and not directly enough.

We know that Array. prototype. slice. call (arguments) can convert objects with the length attribute into an array, except for the node set under IE (because dom objects under ie are implemented in the form of com objects, js objects and com objects cannot be converted)
For example:

 var a={length:2,0:'first',1:'second'}; Array.prototype.slice.call(a);// ["first", "second"] var a={length:2}; Array.prototype.slice.call(a);// [undefined, undefined]

Maybe the kids shoes who just started learning JavaScript didn't quite understand why this sentence can implement this function. For example, I am one, so let's look at it.

First, slice has two usages: String. slice and Array. slice. the first returns a String and the second returns an Array. here we can see 2nd.

Array. prototype. slice. call (arguments) can convert arguments into an array, which is arguments. toArray (). slice (); here, can we say Array. prototype. slice. the call (arguments) process is to convert the first input parameter into an array before calling slice?

Let's look at the call usage, as shown in the following example:

 var a = function(){   console.log(this);  // 'littledu'   console.log(typeof this);   // Object   console.log(this instanceof String);  // true } a.call('littledu');

It can be seen that after the call, the current function is pushed into the scope of the passed parameter. I don't know if this is true, but this points to the object to be passed in.
Here, it is almost the same. We can boldly guess the internal implementation of slice, as shown below:

Array. prototype. slice = function (start, end) {var result = new Array (); start = start | 0; end = end | this. length; // this indicates the called object. When call is used, the point of this can be changed, that is, the point to the passed object. this is the key for (var I = start; I <end; I ++) {result. push (this [I]);} return result ;}

This is probably the case. Just understand it and leave it alone.

Finally, a general function is attached to convert it 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 <len; I ++) {// arr. push (s [I]); arr [I] = s [I]; // It is said that this is faster than push} return arr ;}}
Related Article

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.