Application of the apply function in Function

Source: Internet
Author: User

Recently, when studying the puremvc source code, we found that the apply function in the top-level function package is quite interesting. Through this function, we can promptly call the same function in different classes. This is the function Format: apply (thisarg :*
, Argarray :*
):*


Specify
thisObject
 
. This method also specifies the parameters to be passed to any called function. Because
apply()
 
It is a function class method, so it is also a method for every function object in ActionScript.

And
Function.call()
 
(It specifies the parameter as a list separated by commas). This method specifies the parameter as an array object. This method is usually useful if you cannot know the number of parameters to be passed before the script is actually executed.

Returns the value specified as the return value of the called function.

The following example:

First, define the interface:

Package com. Interfaces
{
Public interface itest
{
Function testapply (showtext: string): void;
}
}

The following are the four classes defined respectively:

T1:

Package com. Custom
{
Import com. Interfaces. itest;

Public class T1 implements itest
{
Public Function T1 ()
{
}

Public Function testapply (showtext: string): void
{
Trace ("T1:" + showtext );
}
}
}

T2:

Package com. Custom
{
Import com. Interfaces. itest;

Public class T2 implements itest
{
Public Function T2 ()
{
}

Public Function testapply (showtext: string): void
{
Trace ("T2:" + showtext );
}
}
}

T3:

Package com. Custom
{
Import com. Interfaces. itest;

Public class T3 implements itest
{
Public Function T3 ()
{
}

Public Function testapply (showtext: string): void
{
Trace ("T3:" + showtext );
}
}
}

T4:

Package com. Custom
{
Import com. Interfaces. itest;

Public class T4 implements itest
{
Public Function T4 ()
{
}

Public Function testapply (showtext: string): void
{
Trace ("T4:" + showtext );
}
}
}

Test in the main class:

Package
{
Import com. Custom. T1;
Import com. Custom. T2;
Import com. Custom. T3;
Import com. Custom. T4;
Import com. Interfaces. itest;

Import flash. display. Sprite;

Public class tfunctionapply extends Sprite
{
Private var testarr: array = new array ();
Public Function tfunctionapply ()
{
This. showtest ();
}

Private function showtest (): void
{
Testarr. Push (New T1 ());
Testarr. Push (New T2 ());
Testarr. Push (new T3 ());
Testarr. Push (New T4 ());

For (var I: Int = 0; I <this. testarr. length; I ++)
{
This. getfun (I). Apply (this. getclass (I), ["success"]);
}
}

Public Function getfun (Index: INT): Function
{
Return (this. testarr [Index] As itest). testapply;
}

Public Function getclass (Index: INT): itest
{
Return this. testarr [Index];
}

Private function testfa (): void
{
T2.apply (this, ["success"]);
}

Private function T2 (showtext: string): void
{
Trace ("T2:" + showtext );
}
}
}

Output result:

T1: Success
T2: Success
T3: Success
T4: Success

Is it very convenient, it is very easy to use in Interface Programming.



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.