Two usages of call in JavaScript _javascript skills

Source: Internet
Author: User

Usage one (common usage):

The expression is: an object. method. Call (another object), meaning to replace the current object with another object and execute the method of the current object. First look at the example:

Copy Code 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 execute

We first define two function, respectively, Class1 and Class2, whose main difference is that Class2 has a showname () method more 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 did you do that? 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 the 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 the fun1 does not need parameters but uses the variables in the parent function, Then require the generation of C1 and C2 function with the same name of those fun1 used variables; 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:

Used in the definition of a function, the expression: another existing function. Call (this), which can clone the variables and methods of another existing function into its own function, implementing a function similar to inheritance. Look at an example:

Copy Code 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 a try

var cat = new Cat ("Black_cat");
Cat.showname (); Alert ("Black_cat")

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.