Go Array.prototype.slice.call Self-Solution

Source: Internet
Author: User
Tags javascript currying

Many frameworks or libraries will use this sentence, most of the Array.prototype.slice.call (arguments,0) to the arguments this pseudo-array into a real array. But why this can be done, but has always been half understand. Last night saw the source of MooTools, Google a bit, finally thoroughly understand.

The call method does not have to say much, Array.prototype.slice.call (arguments,0) is similar to Arguments.slice (0), but because arguments is not a real Array, So it doesn't slice this method. can use the slice method, as long as there is the length property on the line. Although arguments has the length property, there is no slice method, so, Array.prototype.slice () execution, Array.prototype has been called to arguments, because it satisfies the conditions of the slice execution (with the length attribute), so there is no error.

In order to study currying in Javascript, read a lot of foreign language website, to find a better currying scheme, find the more detailed there are the following (the article internal links may contain more related articles):

1. currying JavaScript Functions
2. JavaScript currying Redux
3. JavaScript currying
4. Functional.js Introduction and source code Analysis

Found that most people have used Array.prototype.slice.call (argments,0), has not understood this sentence is what. And yesterday I reviewed the slice () method, and then refer to Function.call (thisarg[, arg1[, arg2[, ...]), still unknown (my brain turns slowly: |). Online Search, the first time to check the foreigner's google.com in the Chinese also did not explain, really to find "Array.prototype.slice usage", only to get a few articles. Although I do not despise the collection of people, make internet articles like the world a big copy, but now hate less, only three (the same three, editor for admin or Super Administrator, the author is anonymous. )

Here is my understanding, the Array.prototype.slice.call to explain, rookie left, shrimp spit:
Update: The following may be a bit difficult to understand, afraid of the first contact with JavaScript may not understand, please read after you leave a message, thank you!
Array.prototype.slice.call (thisarg[, arg1[, arg2[, ...])

Member Introduction:
Array [Object]

Array object (that is, the array object)
Prototype [Property]

JavaScript's prototype, this prototype non-prototype.js, can be understood for the moment as a static property/method in Java. In short, this is not good to say, detailed please see here.
Slice [Function]

Array slicing method, useful for a method, specifically see here, or there.
Call [Function]

The Magic method, the method is the method in all function objects, so slice has such a method. Used to replace this in the function object with the target object. Look here.

String up is:

In human language, there is an array of Prototype,slice is the son of prototype, call is Slice's wife (many bricks flew over.) Please keep looking down.

Effect:
Array.prototype.slice.call = = [].slice

Although the result is a kind of operation, but after the test (others do the test, not me), the former is more efficient than the latter, so the application.

Mechanism:
The slice method here is a function object. Typically, a programmer calls function by using the following method, which is essentially a normal function call method:

Array.slice (Begin,end)

As for the call () method, Method:function::call tells us that you can

Function.call (thisarg[, arg1[, arg2[, ...])

Call a function object and replace the This object with Thisarg, which can get the following code:

Array.prototype.slice.call (Obj,begin,end)

To simulate a process with pseudo-code:

Well, we use pseudo-code to simulate the whole process:

1 var myslice=array.prototype.slice;

2 alert (Myslice); Alert:function (Begin[,end]) {...}, this is a "function"

3 var array_empty=myslice (begin,end); Create a New,empty array

4 set Array_empty=obj; At this point, the obj is assigned to the empty array, so that the array is seeded,
This is what call () does, "artificial fertilization".

5 alert (Array_empty); This is an undefined array object, controlled by the inside of JavaScript,
This may not be a result, but with the result, it must be related to obj.

Here's the key line:
6 var array_last=array_empty.slice (begin,end);
/**
* Although it was previously said to be unshaped,
* But the returned one is already an array, we cut the arrays, but what do we get to cut?
* Yes, cut with an array made of obj,
* A new Array object has been created!
*/




Similarly, Array.prototype.slice.apply (obj) and similar code can be understood as long as they are not disturbed by prototype. If you see people here who don't understand, please revisit Array.slice (begin,end).

Go Array.prototype.slice.call Self-Solution

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.