Call Apply Uses

Source: Internet
Author: User

1. The main difference between call and apply:

Call incoming parameter is an incoming, and apply uses an array of incoming

Call (OBJ,ARG1,ARG2,ARG3,ARG4)

Apply (obj,[arg1,arg2,arg3,arg4]);

2, the use of the scene:

2.1 Method Invocation:

  var say=  function  (msg)        {            console.info ("say:" + msg);        }         var function (_name)        {            this. name=_name;        }                 var New man ("RHYTHMK");        Say.call (M, m.name);         //

Class 2.2 Inheritance:

        varPoper =function(_name) {//Inheritance            /*Call (Replace object, parameter 1, parameter 2, Parameter 3) can also be implemented by call to implement multiple inheritance*/Man.call ( This, _name); }        varp =NewPoper ("PName");        Console.log (P.name); Console.error ("Start verifying the difference between call and apply:"); varAnimal =function(_name, _age, _type) { This. Name =_name;  This. Age =_age;  This. Type =_type;  This. Show =function () {                varmsg = This. Name + ", Age:" + This. Age + ", type:" + This. Type;            Console.log (msg);        }; }        varDog =function(_name, _age) {Animal.call ( This, _name, _age, ' dog '); }        varPig =function(_name, _age) {animal.apply ( This, [_name, _age, ' pig '])        }        varD1 =NewDog ("D1", 12);        D1.show (); varP1 =NewPig ("P1", 10); P1.show ()

Call Apply Uses

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.