The call () method and apply () method of function in ActionScript are described in detail.

Source: Internet
Author: User

The call () method and apply () method of function in ActionScript are described in detail.

Today, we can see the call and apply methods of the function. I checked them online. Some people say these two methods are used to dynamically change the point of the function and give an example:

Import customize. my_class;

Public VaR _ property: string;

Public Function Init (): void
{
VaR FN: function = new function ();
Fn = function (... ARGs): void
{
This. _ property = ARGs;
}

VaR _ OBJ: my_class = new my_class ();
// FN. Call (_ OBJ, "assigning values to attributes ");
FN. Apply (_ OBJ, new array ("assigning values to attributes "));

Trace (_ obj. _ property );
}
============================================
Defined my_class class
Public class my_class
{
Public VaR _ property: string;
Public Function my_class ()
{
This. _ property = "somiok ";
}
}

From the code above, we can see that the FN function is declared in the form of a function expression. In this case, the function point can be dynamically changed (that is, the this keyword of the function changes with the function attachment ).

I tried it in the form of a function statement. The function cannot dynamically change its orientation and can only be used to execute the function.

Import customize. my_class;

Public VaR _ property: string;

Public Function Init (): void
{
VaR _ OBJ: my_class = new my_class ();
// FN. Call (_ OBJ, "assigning values to attributes ");
FN. Apply (_ OBJ, new array ("assigning values to attributes "));
 
Trace (_ obj. _ property );
}

Private function FN (... ARGs): void
{
This. _ property = args. tostring ();
}
============================================
Defined my_class class
Public class my_class
{
Public VaR _ property: string;
Public Function my_class ()
{
This. _ property = "somiok ";
}
}

Later I checked the book of the Black Feather BOSS: the differences between the function statement definition method and the function expression definition method are also reflected in the memory of the this keyword. Using the function statement definition method, the this keyword is firmly directed to the domain defined by the current function. If the function expression definition method is used, the this keyword also changes as the objects attached to the function are different.

To define a statement, you must first declare and then call it.

Conclusion: if the function is defined in the form of a function statement: the call method and the apply method only play the role of executing the function.

If the function is defined in the form of a function expression: the call method and the apply method dynamically change the orientation of the function and change the attributes of the class.

Category: as original green channel: haowen top follow me favorites this article to contact me

Shusheng
Follow-0
Fans-2 follow the blogger 10 (please comment on the article) «previous article: There is a kind of feeling that you can cherish it only when you lose it.
» Next article: Introduction to flash/flex framework-puremvc

Posted @ 2010-06-30 views (201) Comments (0) EDIT favorites

Register

Today, we can see the call and apply methods of the function. I checked them online. Some people say these two methods are used to dynamically change the point of the function and give an example:

Import customize. my_class;

Public VaR _ property: string;

Public Function Init (): void
{
VaR FN: function = new function ();
Fn = function (... ARGs): void
{
This. _ property = ARGs;
}

VaR _ OBJ: my_class = new my_class ();
// FN. Call (_ OBJ, "assigning values to attributes ");
FN. Apply (_ OBJ, new array ("assigning values to attributes "));

Trace (_ obj. _ property );
}
============================================
Defined my_class class
Public class my_class
{
Public VaR _ property: string;
Public Function my_class ()
{
This. _ property = "somiok ";
}
}

From the code above, we can see that the FN function is declared in the form of a function expression. In this case, the function point can be dynamically changed (that is, the this keyword of the function changes with the function attachment ).

I tried it in the form of a function statement. The function cannot dynamically change its orientation and can only be used to execute the function.

Import customize. my_class;

Public VaR _ property: string;

Public Function Init (): void
{
VaR _ OBJ: my_class = new my_class ();
// FN. Call (_ OBJ, "assigning values to attributes ");
FN. Apply (_ OBJ, new array ("assigning values to attributes "));
 
Trace (_ obj. _ property );
}

Private function FN (... ARGs): void
{
This. _ property = args. tostring ();
}
============================================
Defined my_class class
Public class my_class
{
Public VaR _ property: string;
Public Function my_class ()
{
This. _ property = "somiok ";
}
}

Later I checked the book of the Black Feather BOSS: the differences between the function statement definition method and the function expression definition method are also reflected in the memory of the this keyword. Using the function statement definition method, the this keyword is firmly directed to the domain defined by the current function. If the function expression definition method is used, the this keyword also changes as the objects attached to the function are different.

To define a statement, you must first declare and then call it.

Conclusion: if the function is defined in the form of a function statement: the call method and the apply method only play the role of executing the function.

If the function is defined in the form of a function expression: the call method and the apply method dynamically change the orientation of the function and change the attributes of the class.

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.