The call () method in JS and the Apply () method

Source: Internet
Author: User

Excerpt from: http://blog.csdn.net/chelen_jak/article/details/21021101

In the Web front-end development process, we often need to change this point, usually we think of is to use the call method

I. Definition of the call method

The definition of call is a mouthful. In my understanding, A.call (b,arg1,arg2.) Is the method of the A object applied to the B object. For example, the following example:

function Add (b) {alert (a+b);} function Reduce (b) {alert (a-b);} Add.call (Reduce,// Apply the Add method to reduce with a result of 4
second, call can change this point

The following example:

function B () {alert (this//window//window//A 

Let's look at a complicated example:

functionAnimal () { This. name="Animal"; This. showname=function() {alert ( This. Name)}}functionCat () { This. name="Cat";}varAnimal =NewAnimal ();varCat =NewCat (); Animal.showname ();//The result is animalAnimal.showName.call (CAT);//Originally, Cat did not have a ShowName method, but the animal ShowName method was applied to cat using the call method, so the result is cat
iii. realization of inheritance

Here's an example:

function Animal (name) {this. name=name; this. showname=function() {alert (this. Name)}}function  Cat (name) {Animal.call (this// ) applies Animal to Cat, so cat has all the properties and methods of Animal }  varNew// Browser popup black Cat
Iv. use of Apply

The use of apply and call is not the same in one place, but in addition, the other places are basically identical

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

Apply (B,[ARG1,ARG2])//apply has only 2 parameters, which will call the parameters (Arg1,arg2 ... ) placed in an array as the second parameter of apply

Other Summary:

Call Method:
Syntax: Call ([thisobj[,arg1[, arg2[, [,. ArgN]]])
Definition: Invokes one method of an object, replacing the current object with another object.
Description
The call method can be used to invoke a method in place of another object. The call method can change the object context of a function from the initial context to a new object specified by Thisobj.
If the Thisobj parameter is not provided, then the Global object is used as the thisobj.

Apply method:
Syntax: Apply ([Thisobj[,argarray]])
Definition: A method of applying an object that replaces the current object with another object.
Description
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:

functionAnimal (name) { This. Name =name;  This. ShowName =function() {Console.log ( This. Name); };}functionCat (name) {Animal.call ( This, name);} Cat.prototype=NewAnimal (); The//prototype property gives you the ability to add properties and methods to an object. functionDog (name) {animal.apply ( This, name);} Dog.prototype=NewAnimal ();varCat =NewCat ("Black cat");//Call must be an objectvarDog =NewDog (["Black Dog"]);//apply must be an arraycat.showname ();d og.showname (); Console.log (CatinstanceofAnimal); Console.log (doginstanceofAnimal);

The call () method in JS and the Apply () method

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.