The difference between call and applyの

Source: Internet
Author: User

/*/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の

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.