Thoughts on the call mechanism caused by the call () method in JavaScript

Source: Internet
Author: User

Cause:
Today, when I read the top five jQuery (taking DOM elements) of snandy, I can see the toArray () method. The specific jQuery code is as follows:
Copy codeThe Code is as follows:
ToArray: function (){
Return slice. call (this, 0 );
},
Get: function (num ){
Return num = null?
// Return a 'clean' array
This. toArray ():
// Return just the object
(Num <0? This [this. length + num]: this [num]);
},

We can see the call () method here. I have read the manual before, saying that the object is impersonating and used for inheritance. The jQuery source code is a bit messy, so we extract this part and put it in a separate file to see the specific execution.

But it is still not clear. Today I decided to study the call. So I checked the description on the MDN and took out my "sunflower Collection"-niujin dictionary. I am going to practice my English skills and improve my skills, it also provides some help to friends in need (if there are some ways out in translation, please forgive me !)
Call
Abstract:
Call a function by specifying this and arguments.
Note: This method is similar to the apply method syntax, but the difference is that call () accepts the parameter column, while apply () accepts the parameter array passed to the function.
One method of the Function class: Versions later than JavaScript 1.3
Syntax:
Fun. call (thisArg [, arg1 [, arg2 [,...])
Parameter description:
ThisArg:
Specify an object for the call of fun. Note: The value this you see may not be the actual value: if this method is in non-strict mode, null and undefined will be replaced by the global object, the original value is encapsulated.
Arg1, arg2 ,....
Parameters of this object
Description:
When you call an existing function, you can allocate different objects. In this case, the object specified by this is the currently called object.
With call, you can write a method only once and inherit it by another object. This method is rewritten when you do not need to create an object on your own. (Object impersonating. The following is an example !)

See the examples on the MDN official website. In addition, I accidentally saw a related question in stackoverflow. When I saw an answer in it, I suddenly understood how the object was impersonating.
Extract the part below (click here to view the original text ):

In javascript, methods of an object can be bound to another object at runtime. In short, javascript allows an object to "borrow" the method of another object:
Copy codeThe Code is as follows:
Object1 = {
Name: 'frank ',
Greet: function (){
Alert ('hello' + this. name)
}
};
Object2 = {
Name: 'andy'
};
// Note that object2 has no greet method.
// But we may "borrow" from object1:
Object1.greet. call (object2 );

The call and apply methods of function objects (in javascript functions are objects as well) allows you to do this. so in your code you cocould say that the Nodelist is borrowing an array's slice method. what does the conversion is the fact that slice returns another array as it's result.

The first sentence here is very vivid. It roughly means that in JavaScript, the object method can be bound to another object. To put it simply, JavaScript allows an object to 'borrow 'instead of its own method. "Impersonate" is self-evident. In the preceding example, object2 impersonates object1 to call the object1 method.

PS: The first time cainiao wrote a blog is a bit messy. I believe it will be improved in the future. I want to learn from you how to write a blog and write a blog. In addition, you are welcome to give me criticism and guidance!

References:
1. Implementation of the w3cschool ECMAScript Inheritance Mechanism

2. call description on MDN

3. stackoverflow

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.