Call and Apply methods in JavaScript

Source: Internet
Author: User

Ext.: http://www.cnblogs.com/ArthurPatten/p/3335912.html

We can consider call and apply as a method of an object, calling the function indirectly in the form of a calling method. The first argument to call and apply is the parent object that is to invoke the function, which is the calling context through which this is used in the body of the function to obtain a reference to it.

For example, if you want to invoke the function f as a method of object o, you can use the call and apply methods as follows:

F.call (o);

F.apply (o);

The following code can be used to understand:

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.

function Testfun () {

return THIS.A + this.b;

}

var o = {a:1, b:2};

Testfun.call (o); 3

Testfun.apply (o); 3

  The result of the above code execution is 3, which can be understood as return O.A + O.B.

Consider a problem if the first argument of the call and the Apply method is null or undefined? Take a look at the following example:

var a = ten, B = 20;

function Testfun () {

return THIS.A + this.b;

}

Testfun.call ();

Testfun.apply ();

  The result of the above code execution is 30. This is because the first argument of call and apply, if passed in null or undefined, is replaced by the global object.

What is the difference between the two methods of call and apply?

For the call method, all arguments after the first invocation of the context argument are the values to pass in the function to be called. For example, to invoke the function f in the form of the object O method, and pass in two parameters, you can use the following code:

F.call (o, 1, 2);

The Apply method places all arguments after the first argument into an array,

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

Let's take an example.

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, which can be understood as return O.A + O.B + 10 + 20

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.