A detailed description of the JavaScript call function

Source: Internet
Author: User

Call is primarily applied to function objects, requiring JavaScript to be more than 5.5.

The function is to call one method of an object and replace the current object with another object.

Call ([thisobj[,arg1[, arg2[, [,. ArgN]]])

Parameters

Thisobj

Options are available. The object that will be used as the current object.

Arg1, Arg2,, ArgN

Options are available. The method parameter sequence will be passed.

Description

The call method can be used to invoke a method in place of another object. The call method can change the object context of a function from the initial context to a new object specified by Thisobj.

If the Thisobj parameter is not provided, then the Global object is used as the thisobj.

Obj1.method1.call (Obj2,argument1,argument2)

As above, the role of call is to put the Obj1 method on the Obj2 to use, the back of the argument1. These are passed as arguments.

Here are a few examples to illustrate the following:

  function A (x, y) {alert (x+y);  }  function b (x, y) {   alert (x*y);  }  A.call (b,3,4)

As above: This example is a instead of B, A.call (b,3,4) ==a (3,4); Note that the function in JS is an object, and the function name is a reference to the object.

Let's look at an example: the

  function Class1 () {this.name= "Class1"; This.shownam=function () {alert (this.name);}  }  function Class2 () {this.name= "Class2";  }  var c1=new class1 ();  var c2=new class2 ();  C1.showNam.call (C2);

The above meaning is to put C1 method to C2 execution, originally C2 no Shownam method, through call so C2 also have call method. So the result is alert (CLASS2);

Call implementation inheritance, examples are as follows:

  function Class1 () {this.showtxt=function (TXT) {alert (TXT);}  }    function Class2 () {  class1.call (this);  }  var c2=new class2 ();  C2.showtxt ("CC");

So Class2 inherited Class1,class1.call (this), that is, the Class1 object instead of the This object, then Class2 there is no C1 all properties and methods? The C2 object is able to invoke the Class1 method and properties directly, and the result is: alert ("CC");

In fact, call can also achieve multiple inheritance:

  function Class10 () {this.showsub=function (A, A,) {alert (A-B);}  }  function Class11 () {this.showadd=function (A, b) {     alert (a+b);}  }   function Class2 () {Class10.call (this), Class11.call (this);}  var a=new class2 (); A.showsub (3,4); A.showadd (4,5);

In this way, double inheritance is achieved.


A detailed description of the JavaScript call function

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.