Call && Apply Usage

Source: Internet
Author: User

.  Each function contains two non-inherited functions: call,apply. Calling a function in a specific scope is actually equivalent to the value of this object in the body of a function

One use of the. Call,apply is to pass parameters . In fact, the powerful place is: Expand the scope of the function

EG1: ( pass parameters )

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function sum(x, y) {

return x+y;

}

function call1(n1,n2) {

return sum . Pager          ( this, n1,n2); ~ ~ ~ Return sum. Apply (This, [N1, N2] );

}

Alert (call1(10,20)); Alert (30);

 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

EG2: ( extended scope ) Benefits: There is no coupling between objects and methods

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Window.color = ' red ';

var obj = {color: ' Blue '};

function Showcolor () {

alert (This.color);

}

showcolor.call (window);   // Showcolor.call (this); //Red

Showcolor.call (obj); //blue

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//Custom Object

function Obj (x, y) {

this.x = x;

This.y = y;

return x*y;

}

//Method

function Fun (A, b) {

return a + B;

}

var obj = new obj (10, 20);

Alert (fun-Call (obj, obj.x, obj.y); //

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~













Give a few specific examples:


Example 1:

Function Add (A, b)

{

alert (A+B);

}

function Sub (A, b)

{

Alert (A-B);

}

Add.call (sub,3,1);

Add.apply (sub,[3,1]);






Example 2:

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); Class2

C1.showNam.apply (C2);




Example 3:


var func=new function () {this.a= "Func"};

//var func = { a: "Func"};


var myfunc=function (x) {

var a= "MyFunc";

alert (THIS.A);

alert (x);

};

Myfunc.apply (func, "Var");

Myfunc.apply (func,["var"]);




Example 4:[hanging explosion days have wood ~call can be used to achieve inheritance]


function Class1 ()
{ 
this.showtxt = function (TXT)
    { 
alert (TXT);
    } 
} 
function Class2 ()
{ 
Class1.call (this);
} 
var c2 = new Class2 ();
c2.showtxt ("cc");



Example 5: Multi-inheritance [how javaScript simulates object-oriented inheritance, but also enables 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 C2 = new Class2 ();

C2.showadd (1,4); 5

C2.showsub (10,6);//4












Call && Apply Usage

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.