Call () and apply () in JavaScript

Source: Internet
Author: User

This weekend do not work, on their own dormitory do not finish before the JS online programming problem, met a question about the function of the argument, all of a sudden Meng Quan, because they have done most of the programming problems of the kind of grammar, and like this rarely met, suddenly there is no way of thinking, the topic requirements as shown in the following figure:

Then I looked at the code that the site had passed, found that everyone's program using the Apply () method, remember to have contact with this method before, but just around, did not want to go deep to understand, and then went online to look up the information, find a blog to speak call () and apply () Bowen 1, method to define the call method:

Grammar : Obj.call (Thisobj, arg1, arg2, ...);
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 instead of another object. The call method can change the object context of a function from the initial context to the new object specified by Thisobj.
If the thisobj parameter is not supplied, the Global object is used as a thisobj. Apply method:

Grammar : obj.apply (Thisobj, [Arg1, Arg2, ...]);
definition : Applies one method of an object and 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 you do not supply any of the Argarray and thisobj parameters, the Global object will be used as a thisobj and cannot be passed any parameters. 2. Common examples

function Add (a,b)  
{  
    alert (a+b);  
}  
function sub (a,b)  
{  
    alert (a-b);  
}    
Add.call (sub,3,1);   

The meaning of this example is to replace sub,add.call (sub,3,1) = Add (3,1) with add, so the result is: alert (4);
Note: The function in JS is actually an object, the function name is a reference to a function object.

function Animal () {    
    this.name = "Animal";    
    This.showname = function () {    
        alert (this.name);    
    }    
}    

function Cat () {    
    this.name = "Cat";    
}    

var animal = new animal ();    

Using the call or Apply method, the ShowName () method originally belonging to the animal object is given to Object cat.
Enter the result as "Cat"
Animal.showName.call (Cat, ",");
Animal.showName.apply (cat,[]);
Call means to put the animal method to carry out the cat, the original cat is no ShowName () method, now is the animal ShowName () method put to cat to execute, so this.name should be cat

Implementing inheritance

function Animal (name) {      
    this.name = name;      
    This.showname = function () {      
        alert (this.name);      
    }      
}          
function Cat (name) {    
    Animal.call (this, name);    
}      
var cat = new Cat ("Black Cat");     
Cat.showname ();  

Animal.call (this) means to use the Animal object instead of this object, then the cat does not have all the Animal properties and methods, the Cat object can directly call the Animal method and properties.

Multiple inheritance

function Class10 ()  
{  
    this.showsub = function (a,b)  
    {  
        alert (a-b);  
    }  
}  

function Class11 ()  
{  
    This.showadd = function (a,b)  
    {  
        alert (a+b);  
    }  
}  

function Class2 ()  
{  
    class10.call (this);  
    Class11.call (this);  
}  

It's easy to use two call to achieve multiple inheritance.

Said call, and of course, apply, the two methods are basically a meaning, the difference is that the second parameter of call can be any type, and the second parameter of apply must be an array, or it can be a arguments

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.