The difference between the call, apply, and bind methods in JavaScript

Source: Internet
Author: User

In JS, this is the direction of the dynamic change, it is likely in the process of writing programs, inadvertently destroy the point of this, so we need a can be the meaning of this fixed technology, so there are call,apply and bind these three methods

Apply: One method of applying an object that replaces the current object with another object.
Call: Invokes one method of an object, replacing the current object with another object.

What they have in common:
can be used instead of another object to invoke a method that changes the object context of a function from the initial context to the new object specified by Thisobj.

The difference between them:
①apply: There can be a maximum of two parameters-the new this object and an array Argarray. If you pass multiple arguments to the method, the arguments are written into the array, and of course, even if there is only one argument, it is written into the array. If Argarray is not a valid array or is not a arguments object, it will result in a TypeError. If none of the Argarray and Thisobj parameters are provided, then the Global object is used as a thisobj and cannot be passed any parameters.

code example

Fun.call (thisarg[, arg1[, arg2[, ...]) function f (x, y) {  console.log (x+y);} F.call (NULL, 1, 1)//return 2

②call: is a direct parameter list, mainly used in the JS object each method calls each other, so that the current this instance pointer is consistent, or in special cases need to change the this pointer. If the Thisobj parameter is not provided, then the Global object is used as the thisobj.

code example

Fun.apply (Thisarg, [Argsarray]) function f (x, y) {  console.log (x+y);} F.call (null, [+])//return 2

Summary
The Apply and call functions are just the same as the parameter list passed in

③bind

Fun.bind (thisarg[, arg1[, arg2[, ...])

Unlike the above, BIND returns a new function that changes this point, note that the new function is highlighted here, and it is not the same block memory address used previously, so when you need to reuse the function, you have to save it to a variable for the next call. The above two functions are the result of the execution of the return, that is, the call is executed!

Also, it is important to note that the first parameter in the BIND function automatically becomes the default value for the parameter returned in the new function, so when the call is formally called, only the remaining parameters can be given except the first parameter.

code Example
function f (x, y) {  console.log (x+y);} F.call (null, [+]) var new_f = F.bind (null,1,1)//return new function New_f (1)//return 2

It should be explained that the Thisarg parameter in all the sample code above is replaced with NULL, in the case where the specified Thisarg object is not given, null and undefined under this point is the global object, that is, the JS code execution environment.

The difference between the call, apply, and bind methods 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.