/*/function Add (b) { alert (a+b);} function 3, 1); // Alert 4
/** Call means to put animal's ShowName method on Cat, so the result is: alert (' Cat ');*/functionAnimal (name) { This. Name = ' Animal '; This. ShowName =function() {alert ( This. Name); }}functionCat (name) { This. Name = "Cat";}varAnimal =NewAnimal ();varCat =NewCat (); animal. Showname.call (cat);
/** * Animal.call (this) means that the Animal object is used instead of the this object, so there are no Animal properties and methods in Cat, and the Cat object is able to invoke the Animal method and properties directly. **/functionAnimal (name,name2) { This. Name =name; This. name2 =name2; This. ShowName =function() {alert ( This. Name); Alert ( This. name2); }}functionCat (name1,name) {Animal.call ( This, Name1,name);}varCat =NewCat (' I'm a tiger ', ' I'm a sick cat '); Cat. ShowName ();
/** * Class2 also inherit CLASS10 and Class11 **/functionClass10 () { This. Showsub =function(A, b) {alert (a-b); }}functionClass11 () { This. Showadd =function(A, b) {alert (a+b); }}functionClass2 () {Class10.call ( This); Class11.call ( This);}
Class2 ();
Cls2. showsub (5,2); Alert (3)
Cls2. Showadd (7,2); Alert (9)
Next is the usage and understanding of apply
/** *apply: Method can hijack another object's method, inherit another object's property. *call: Like apply, it's just a different argument list. *. call (obj, arg1, arg2, Arg3, ...); *. Apply ( Obj,[arg1, arg2, Arg3, ...]) *//functionClass10 (name1,name2) { This. name1 =name1; This. name2 =name2; This. ShowName =function() {alert (' Name1: ' + This. name1); Alert (' Name2: ' + This. name2); }}functionClass11 (name1,name2,age) { This. Age =Age ; Class10.apply ( This, arguments);//here, Class11 hijacked the properties of the CLASS10 [Name1,name2,age] with the method ShowName (); This. Showage =function() {alert (' Age: ' + This. Age); }}varCLS =NewCLASS11 (' Name 1 ', ' Name 2 ', ' several years old ')); CLS. ShowName (); CLS. Showage ();
Most of the code is to find the information on the Internet, and then make a small change of their own, test through, easy to understand-w-
Easy-to-understand format:
Parent class. Call (subclass);
Parent class. Apply (subclass, arguments);
As opposed to C #:
Class Subclass: Parent class {...}
The difference between call and applyの