Two usages of call in JavaScript

Source: Internet
Author: User

  This article mainly introduces two usages of call in JavaScript, and a friend in need can refer to the

Usage one (common usage): The   manifestation is: an object. method. Call (another object), meaning to use another object instead of the current object to execute the method of the current object. First look at the example:   code as follows: function Class1 () {    this.name = "Class1";     this.showname = function () {        alert (this.name);    } function Class2 () {    this.name = "Class2";} var c1 = new Class1 (); var C2 = new Class2 ();    C1.showName.call (C2); C2.showname ();        //cannot perform       We first defined two function, respectively, CLASS1 and Class2, The main difference is that Class2 has a showname () method than Class1. Then we define the objects corresponding to CLASS1 and Class2 C1 and C2, and we are aware that C1 has ShowName () method and C2 does not. But miracles happen, and when we execute C1.shoName.call (C2), we pop up the C2 name value, which is "Class2." In fact, we are still the implementation of the C1 method, but only on the whim of the object C2 stolen objects C1, after the execution, they are still the definition of the appearance, C2 is not so much more than what method. In order to detect C2 There are no more methods, the example adds a row c2.shownmae (); It can not be executed, the browser will report the Object #<class2> has no method ' ShowName ' error.   Why do you do this? As I said before, this is a temporary way to use, we are using its efficient programming just. However, this is not unrestricted, assuming that C1 and C2 represent replaced objects and replacement objects, and fun1 represent C1 intrinsic methods. 1, when fun1 do not need parameters, and do not have to the parent function of any local variables, in fact, C1.fun1.call (C2) and c1.fun1 () no difference; 2, when FUN1 does not needTo parameters but to the variables in the parent function, it is required to generate the variables that the C1 and C2 functions have the same name for fun1 use; 3, when the fun1 need parameters, the form should be rewritten into C1.fun1.call (c2, parameter 1, parameter 2, ...). Parameter n, the variable name in the function that generates the C1 does not have to have the same name as the variable name of the function that generated the C2, just the corresponding. In fact, when we use this use of call, C2 and C1 tend to have a great similarity in structure and function, so the above three points are easy to avoid.   Usage Two:   use in the definition of function, manifestation: Another already existing function. Call (this), which can clone another existing function's variables and methods into its own function, implementing a function similar to inheritance. See example:   code as follows: function Animal (name) {    this.name = name;     This.showname = function () {        alert (this.name);    }};   var animal = new Animal ("Small_animal"); Animal.showname ();    //alert ("small_animal")   function Cat (name) {    Animal.call (this, name);  //var Animal = null;    //Uncomment try   var cat = new Cat ("Black_cat"); Cat.showname ();    //alert ("Black_cat")  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.