Apply, call__ "JavaScript Advanced Programming" Third edition

Source: Internet
Author: User
Apply, call

In JS, call and apply are meant to change the context in which a function is run, in other words, to change the this point within the function body.
A big feature of JS is the notion that the context and runtime context and context can be changed when the function is defined.

function fruits () {}

fruits.prototype = {
  color: ' Red ',
  say:function () {console.log (' my color is ' + This.color);}

var apple = new Fruits;
Apple.say (); ' My color is red '

If we have an object banner = {color: ' yellow '}, we do not want to redefine the say method, then we can call or apply to invoke the Apple object is the Say method:

var banana = {color: ' yellow '};

Apple.say.call (banana)//' My color are yellow '
apple.say.apple (banana);//' My color is yellow '

So it can be seen that call and Apple appear to dynamically change this, when an object does not have a method (the Banana object has no say method), but other objects have (the Apple object has the Say method), We can use call or apply to invoke other objects to manipulate the method. the difference between apply and call

For apply, call both, the function is exactly the same, but the way to accept parameters is not the same.

var func = function (arg1,arg2) {}

You can invoke it in the following ways:

Func.call (THIS,ARG1,ARG2);
Func.apply (This,[arg1,arg2]);

This is the context you want to specify, it can be any JS object, call needs to pass the parameters in order, and apply is to put the parameters in the array.

In JS, a function of the number of parameters is not fixed, so to say the applicable conditions of the painting, when your parameters are clearly know the number of the call when you are not sure of the parameters of individual use apply, and then push the parameters into the array.
When the number of arguments is indeterminate, the function can also be traversed by arguments this array.

To reinforce memory, here are some common methods:
1. Append between arrays

var arr1 = [, ' foo ', {name: ' Joe '},-2458];
var arr2 = [' Doe ', 555,100];
Array.prototype.push.apply (ARR1,ARR2);
ARR1 values are: [, ' foo ', {name: ' Joe '},-2458, ' Doe ', 555,100]

2. Get the maximum and minimum values in the array

var numbers = [5,458,120];
var maxnumbers = Math.max.apply (math,numbers); 458
maxnumbers = Math.max.call (math,5,458,120);//458

Number itself does not have the Max method, but math has, we can use call or apply the method.

3. Verify that the array (if the ToString () method has not been overridden)

function IsArray (obj) {return
  Object.prototype.toString.call (obj) = = ' [Object Array] ';
}
deep Understanding using apply, call

Define a log method so that it can proxy console.log

function log (msg) {
  console.log (msg);
}

Log (1); 1
log (2);//2

The above method is not valid when the incoming argument is personally indeterminate. This is the time to consider using apply.

function log () {
  console.log.apply (console,arguments);
};

Log (1);//1
log (1,2);//1 2
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.