Talking about call and apply in JavaScript

Source: Internet
Author: User

Grammar
Fun.apply (Thisarg, [Argsarray]) Fun.call (Thisarg, arg1, arg2, ...)

Apply receives two parameters, the first parameter specifies the pointer to the this object in the function body, and the second parameter is an indexed collection, which can be an array or a class, and the Apply method passes the total element of the collection to the called function.

Call is the syntactic sugar that is wrapped on apply, and if you know how many parameters you have, you can pass the parameter by using called, which is convenient for expressing the correspondence between formal parameters and arguments.

Often apply and call easy to remember confused, may wish to use associative memory:

Apply->array  // The letter begins with a

as long as you see apply, you will expect to receive an array. apply receives an array or class array object, and call receives the parameter list.

Use

1. Change the direction of this

The simple thing is that when the fun function runs, the value of this specified is Thisarg.

var a = 10,b =function  Add (c,d)     {returnthis this . B + C +D;}        var o ={a:1,b:2};add (//  + + 3 + 4 = PNS//  1 + 2 + 3 + 4 = Ten//  1 + 2 + 3 + 4 = Ten


The above example can be seen

When add is called directly, this points to the window;
When you use call and apply, this points to O;

2. Borrowing other object methods

When a single Object object does not have a method, it can be implemented using call or Apply methods of other objects.

functionCat () {}cat.prototype={voice:"Miao~miao~", say:function() {Console.log ("I can say" + This. Voice); }};varWritecat =NewCat (); Writecat.say (); //I can say miao~miao~//DogvarYellowdog = {voice: "wang~wang~"};writecat.say.call (Yellowdog); //I can say wang~wang~

About Thisarg

Thisarg is the object type.
When the specified Thisarg is not an object type, it is first converted to the appropriate object type, followed by subsequent programs.

function foo (x, y) {    console.log (x, y,this);}    Foo.call (100,1,2);   // 1 2 Number {}Foo.call (true, up to);  // 1 2 Boolean {} //   1 2 String {0= "s",  1= "T",  2= "R",  more ...} Foo.call (null);      // undefined undefined Windowfoo.call (undefined);  // undefined undefined Window

When specified as null and undefined, this in the function points to the default host object, which is window in the browser, but does not convert when in strict mode.

Talking about call and apply in JavaScript

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.