Javascript call functions and javascript Functions

Source: Internet
Author: User

Javascript call functions and javascript Functions

Call is mainly used for function objects. javascript must be above 5.5.

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

Call ([thisObj [, arg1 [, arg2 [, [,. argN])

Parameters

ThisObj

Optional. Will be used as the object of the current object.

Arg1, arg2, and argN

Optional. The method parameter sequence will be passed.

Description

The call method can be used to call a method instead of another object. The call method can change the object context of a function from the initial context to the new object specified by thisObj.

If the thisObj parameter is not provided, the Global object is used as thisObj.

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

As shown above, the call function is to put the obj1 Method on obj2 for use, and the argument1. these are passed as parameters.

The following are examples:

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

As shown above: In this example, a replaces B, a. call (B, 3, 4) = a (3, 4); // note that functions in js are objects and function names are references to objects.

Let's look at another example:

  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 means to put the c1 method into c2 for execution. Originally, c2 had no showNam method, so c2 had the call method through call. The result is alert (class2 );

Call implementation inheritance, for example:

 

  function class1(){this.showTxt=function(txt){ alert(txt);}  }    function class2(){  class1.call(this);  }  var c2=new class2();  c2.showTxt("cc");

In this way, class2 inherits class1, class1.call (this), and replaces the class1 object with this object. Then, does class2 have all the attributes and methods of c1? The c2 object can directly call the Class1 Method and Its Attributes. The execution result is: alert ("cc ");

In fact, call can also implement multiple inheritance:

 

  function class10(){this.showsub=function(a,b){ 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, dual inheritance is realized.


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.