Call and apply in Javascript

Source: Internet
Author: User

In js, call and apply are used to bind functions to another object for running. The two functions are different only when defining parameters, next I will introduce the call and apply usage.

In the web Front-end development process, we often need to change this point. Generally, we want to use the call method, but many people do not have a clear understanding of the call, the following small series will give you a detailed description of call and apply.

1. Definition of the call Method

You can search for call in Baidu. The definition of call is very easy. In my understanding, a. call (B, arg1, arg2.) is the method of object a applied to object B. For example:

The Code is as follows: Copy code

Function add (a, B)
{
Alert (a + B );
}
Function reduce (a, B)
{
Alert (a-B );
}
Add. call (reduce, 1, 3) // apply the add method to reduce. The result is 4.

2. call can change this point

For example:

The Code is as follows: Copy code

Function B ()
{
Alert (this)
}
B (); // window
B. call (); // window
B. call ("a", 2, 3); //

Let's look at a complicated example:

The Code is as follows: Copy code

Function Animal ()
{
This. name = "animal ";
This. showName = function ()
{
Alert (this. name)
}
}
Function Cat ()
{
This. name = "cat ";
}
Var animal = new Animal ();
Var cat = new Cat ();
Animal. showName (); // The result is animal.
Animal. showName. call (cat); // The original cat has no showName method, but the showName method of animal is applied to cat through the call method. Therefore, the result is cat

3. Implement inheritance

Example:

The Code is as follows: Copy code

Function Animal (name)
{
This. name = name;
This. showName = function ()
{
Alert (this. name)
}
}
Function Cat (name)
{
Animal. call (this, name); // apply Animal to Cat, so Cat has all attributes and methods of Animal.
}
Var cat = new Cat ("Black Cat ");
Cat. showName (); // Black Cat is displayed in the browser.

Iv. apply usage

The usage of apply and call is different from that of call.

A. call (B, arg1, arg2 ...)

Apply (B, [arg1, arg2]) // apply has only two parameters, which call the parameters (arg1, arg2 ...) Put it in an array as the second parameter of apply

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.