The call and apply methods in JavaScript talking about _javascript skills

Source: Internet
Author: User
The first argument for call and apply is the parent object that is to invoke the function, which is the invocation context, which is used in the function body to obtain a reference to it.
For example, if you want to invoke the function f with the object O method, you can use the call and apply methods as follows:
Copy Code code as follows:

F.call (o);
F.apply (o);

The following code can be used to understand:
Copy Code code as follows:

O.M = f; Temporary method to store F as O
O.M (); Call this temporary method
Delete o.m; Remove this temporary method

Let's take an example.
Copy Code code as follows:

function Testfun () {
return THIS.A + this.b;
}
var o = {a:1, b:2};
Testfun.call (o); 3
Testfun.apply (o); 3

The above code execution results are 3, can be understood as return O.A + O.B.
Consider a question, what if the first argument of the call and apply method is null or undefined? Look at one of the following examples:
Copy Code code as follows:

var a = ten, B = 20;
function Testfun () {
return THIS.A + this.b;
}
Testfun.call ();
Testfun.apply ();

The result of this code execution is 30. This is because the first argument of call and apply, if passed in null or undefined, is replaced by a global object.
What is the difference between the two methods of call and apply?
For the call method, all arguments after the first invocation context argument are the values to pass in to the function to be called. For example, to call function f in the form of an object o, and pass in two parameters, you can use the following code:
Copy Code code as follows:

F.call (o, 1, 2);

The Apply method then puts all the arguments after the first argument into an array,
Copy Code code as follows:

F.apply (O, [1, 2]);

Let's take an example.
Copy Code code as follows:

function Testfun (x, y) {
Return THIS.A + this.b + x + y;
}
var o = {a:1, b:2};
Testfun.call (o, 10, 20);
Testfun.apply (o, [10, 20]);

The execution result of the above code is 33, can be understood as return O.A + O.B + 10 + 20
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.