Apply, call, bind in Javascript

Source: Internet
Author: User

Address: Apply, call, bind in the language of Javascript

Apply, call

In JavaScript, call and apply are meant to change the context in which a function is run, in other words, to change the direction of this within the function body.

A major feature of JavaScript is the notion that a function has a "context of definition" and a "runtime context" and a "context can be changed."

First, a chestnut:
EG1:

 function   fruits () {}fruits.prototype  =" Red ",say:  function   () {Console.log (" My color is "+ this  .color);}}  var  apple = new   Fruits;banana  = {color:  "Yellow" }apple.say.call (banana);  // my color is yellow  Apple.say.apply (banana); // my color is yellow  



But if we have an object banana= {color: "Yellow"}, we don't want to redefine it say method, then we can use call or apply with Apple's say method.

So, you can see that call and apply are to change this dynamically, when an object does not have a method (this pest banana no say method), but the others have (this pest Apple has say method), We can use call or apply to manipulate other object methods.


The difference between apply and call

For both apply and call, the function is exactly the same, except that the parameters are accepted in a different way. For example, there is a function defined as follows:

var function (Arg1, arg2) {};


You can invoke the following method:

Func.call (This, arg1, arg2); Func.apply (This, [Arg1, arg2])


Where this is the context you want to specify, he can be any JavaScript object (everything in JavaScript), call needs to pass parameters in order, and apply is to put the parameters in the array.

In JavaScript, the number of arguments for a function is not fixed, so if you want to apply the condition, call is used when your parameters are clearly known.

Use apply when unsure, and then pass in the parameter push into the array. When the number of arguments is indeterminate, the function can also traverse all the arguments by arguments the array.

After replying to this article, I thought:


Question 1:

EG2:

var numbers = [5, 458, -215 ]; Math.max.apply (numbers); -infinity


Bind Math.max to numbers why not?

This way, however, you can:

EG3:

var numbers = [5, 458, -215 ]; var // 458


Analysis:
When an empty object is declared arbitrarily:

EG4:

var numbers = [5, 458, -215 ]; var obj =//458


Therefore, it can be seen that the Math.max method is required to pass the parameter, and the array that needs to be compared numbers is not the object obj as this point, but is passed in as a parameter of the comparison. Unlike EG1, parameters are required.

Question 2:

How long is the object method inheritance valid? Is it always owned after inheritance? Or is there only call, when the Apply method runs?

EG5:

function="Red"function() {console.log (this. Color );}} var New  ="Yellow"//My color is yellowconsole.log (banana); // object{color: "Yellow", __proto__: Object}


Analysis:
Banana There is no say method. So, with call, the Apply method inherits after a period of time, only in the function "Runtime context" is valid.

Apply, call, bind

in Javascript

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.