Talking about call, apply, bind

Source: Internet
Author: User

Each function contains two non-inherited methods: Apply () and call (). The purpose of both methods is to invoke the function in a specific scope, which is actually equivalent to setting the value of the This object in the function body.
First, the Apply () method receives two parameters: one is the scope in which the function is run, and the other is the parameter array. Where the second argument can be an instance of an Array, or it can be an arguments object.

function sum (NUM1, num2) {
return NUM1 + num2;
}
function CallSum1 (NUM1, num2) {
Return sum.apply (this, arguments); Incoming Arguments Object

}
function callSum2 (NUM1, num2) {
Return sum.apply (this, [Num1, num2]); Incoming array
}

Alert (CALLSUM1 (10,10)); 20
Alert (callSum2 (10,10)); 20


The call () method has the same effect as the Apply () method, and they differ only in the way that the parameters are received. For the call () method, the first parameter is the this value does not change, changing the rest of the parameters are passed directly to the function. In other words, when using the call () method, the arguments passed to the function must be enumerated individually, as shown in the following example.

function sum (NUM1, num2) {
return NUM1 + num2;
}
function Callsum (NUM1, num2) {
Return Sum.call (this, NUM1, num2);

}
Alert (Callsum (10,10)); 20

The official interpretation of call (), " invokes one method of an object, replacing the current object with another object . "Apply likewise.

Call is a method that does not have a in the C.a.call (b) B object, but wants to use the A method, and then uses call to replace the C object with the A method with the B object.

The nature of the call method implementation process:

Object A. Method. Call (Object b,1,2,3)
Equivalent to
Object B. Method = Object A. method;
Object B. Method (All-in-all);
Delete Object B. method;

In Sum.call (THIS,NUM1) above, I see that sum is not a method of an object but you can print window.sum sum is actually a method of the window global object.
Just like function Aa () {

this.age=18;

}

Aa (); How do we get access to this age attribute? You can window.age that means AA (), This=window.

At this point the extension can be quite natural and the constructor is different.
var a=new Aa (); Constructors are best capitalized to start with, facilitating code reading
a.age=18;

In fact, passing parameters is not the real place to apply () and call (); their real strength is the ability to expand functions
The scope on which to run. Then continue to see how call is used:

Window.color = "Red";

function Saycolor () {
alert (This.color);
}

Saycolor (); Red
Saycolor.call (this); Red can print out this as the Window object
Saycolor.call (window);  Red These two essentially replace the Window object with a Window object. Isn't it a little bit around? Consider the following two will also output red
Window.sayColor.call (window);//red
Window.sayColor.call ();//red
In fact, we don't need to replace the Window object with the Window object, and you can call the Saycolor method in place of any object instead of the Window object.
var a={};
Window.sayColor.call (a);//undefined a object invokes the Saycolor method of window, but the a object itself does not have a color property, which causes alert (This.color) ==undefined

Define a color property directly in the A object
var a={color: "Blue"};
Saycolor.call (a);

Either add a color property to the prototype object of a constructor, and then instantiate an object with the constructor function.
function Test () {};
Test.prototype.color= "Blue";
var test=new test ();
Saycolor.call (test)//blue


This is great for prototyping and prototyping chains. First in test, not to see, then go to test.__proto__ up to find can print out their own test and test.__proto__ at a glance!

Extended scope and scope chain here

function aa () {
This.name= "Wang Wenxuan";

}
function bb () {
This.sayname= function () {
Console.log (THIS.name)
}

}

var a=new aa ();
Bb.call (a); In this call, the A object has all the methods in WINDOW.BB
A.sayname (); Call is simply called but has the Sayname method.


var a=new aa ();
var b =new bb ();
B.sayname.call (a);


var a=new aa ();
var b =new bb ();
var objectsaycolor = B.sayname.call (a); run directly will output the result
var objectsaycolor = B.sayname.bind (a); Run separately
Objectsaycolor (); Will come out of the results.

In fact, bind () generates a new function, which is used inside the function. Call ()/. Apply ().
Source:

Also attached is the magical magic of Call and bind: https://www.zhihu.com/question/40892203 know @ Mona Lisa;

Talking about call, apply, bind

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.