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