Learning about as3 cainiao-function (2): function reference

Source: Internet
Author: User

Function references are often used in programming. Because a function belongs to a complex data type, functions, like classes, operate data through references.

The syntax for referencing a function is to use the function name. For example, the receiver function in an event is actually implemented through reference.

The following code defines the onclick function. This function has a parameter E, whose data type is mouseevent, and void indicates that this function has no return value:

MC. addeventlistener ("click", onclick );
Function onclick (E: mouseevent): void
{
MC. x = mc. x + 5;
}

Check the first line of code:
MC. addeventlistener ("click", onclick );
The function or method addeventlistener () has two parameters:
Click 1st parameters, indicating that the event name is clicked,
The first parameter onclick is the function name. That is to say, the second parameter passes the reference of the function.

Similarly, when using the setinterval () function

Function Test (){
Trace ("function called ");
}
Setinterval (test, 1000); test in this line is a reference to the previously defined function.

Therefore, functions can be used as parameters because functions are complex data types, and any data type variables can be used as function parameters.

Example of using a function as a parameter:

// Call a function
Referfunc (tracemsg );
// Define a function
Function tracemsg (): void {
Trace ("this is function ");
}
// Function reference as a parameter
Function referfunc (F: function): void {
F ();
}

In function referfunc (), a function-type parameter is defined, that is, a function-type parameter, and then the function name f and () are called. This means the function name is used as the parameter.

Example of passing a function as a parameter:

// Call a function
Referfunc (tracemsg, "this is function ");
// Define a function
Function tracemsg (MSG: *): void
{
Trace (MSG );
}
// Reference a function as a parameter and pass the parameter of the referenced Function
Function referfunc (F: function, MSG: *): void
{
F (MSG );
}

Using a function as a parameter actually calls the function f (MSG) in the function, and the parameter F is a reference to the function tracemsg.

The function name is a reference. There can be multiple references, so you can reference the function through variables.

// Reference a function with the variable func
VaR FUNC: function = tracemsg;
// Call a function through reference
Func ();
// Define a function
Function tracemsg (): void
{
Trace ("this is function ");
}

The code above is simplified:

// Define variables and reference functions
VaR FUNC: function = function (): void
{
Trace ("this is function ");
}
// Call a function
Func ();

The code first defines the func variable of the function type, and assigns the reference value of the function to the variable func through the "=" operator.

Tip: the function in the Code does not have a function name. Such a function is called an unknown function. Because there is no function name, you can only call this function by referencing it.

Note: When using a variable to reference a function and call a function, you must first define the variable before calling the function, because the variable must be defined before it can be used.

When a variable of complex data type is used, the two variables are equal only when the variable references the same object. Similarly, only when two variables reference the same function will the two variables be equal.

// Variable func1 reference function tracemsg
VaR func1: function = tracemsg;
// Variable func2 reference function tracemsg
VaR func2: function = tracemsg;
// Compare the references of two variables to see if they are equal
Trace (func1 = func2 );
Function tracemsg (MSG: *): void
{
Trace (MSG );
}

Test code. The result is true, indicating that the two variables reference the same function.

Learning about as3 cainiao-function (2): function reference

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.