Stupid but lucky as3

Source: Internet
Author: User

As3 = actionscript3

Flex supports an interesting template column to generate a list of content rich. Usage example:

 

  1. <Mx: datagridcolumn id = "testcolumn">
  2. <Mx: itemrenderer>
  3. <Mx: component>
  4. <Mx: button label = "test" Click = "alert. Show ('hello')"/>
  5. </MX: component>
  6. </MX: itemrenderer>
  7. </MX: datagridcolumn>

This is a grid column. According to the above description, the grid in the column of each row is a press.

 

However, the preceding token cannot run because "1120: the access attribute alert is not defined. ", Which indicates that we cannot click = xxx.

 

During the design period, the event handling proxy seems to be okay. We can see that the idea is to create itemrender during runtime. You can find that itemrender requires an ifacloud. As in the previous example, what is the itemrender responding. This is easy to understand. Just click a button and copy the following function to the click event:

  1. Function Test (): void {alert. Show (describetype (testcolumn. itemrender. newinstance (). toxmlstring ();}

As a result, a long XML text is generated, and the conversation box is edited. Press ctrl A to copy the text to the notebook, A section of type = "????? On this surface, flex creates a new class for mx: component connector. As mentioned in the preceding figure, flex records each mxaml file as an as file, where each plane automatically generates a category. Flex is also responsible for processing the template metadata (mx: component effect ). However, flex does not reference the package where alert is located when creating the template category. Therefore, it cannot be used by the compiler.

 

After checking the problem, the mobile operator will perform the operation on his/her own:

  1. Testcolumn. itemrenderer = new classfactory (button );

Click Export. However, as a result, only some definitions can be provided to the lattice, and the process of creating a pay-as-you-go statement cannot be used. In this way, not only does the click function fail to be set, the external label cannot be set.

 

Check the source code of classfactory and find that the original class still allows providing experimental packages to initialize the initialization. The source code is as follows:

 

  1. Public var properties: Object = NULL;
  2. Public Function newinstance ():*
  3. {
  4. VaR instance: Object = new generator ();
  5. If (properties! = NULL)
  6. {
  7. For (var p: String in properties)
  8. {
  9. Instance [p] = properties [p];
  10. }
  11. }
  12. Return instance;
  13. }

 

Yes. The following generation is saved:

 

  1. VaR buttoncreator: classfactory = new classfactory (MX. Controls. Button );
  2. Buttoncreator. properties = {Click: function (){
  3. Alert. Show ('Hi ');
  4. }};
  5. Testcolumn. itemrenderer = buttoncreator;

Result of operation :(

 

Click is an event, not an alert, and cannot be a timeout value.

 

The road mania (the turn of the road anger is too open) in the car is said, we are also suffering from mania, TMD.

 

Why did we say that as3 has taken the path of OOP? Can we use the new ifacloud () {...} method to construct an anonymous internal class? The conclusion is, no.

 

What is stupid design? This is stupid design. Is engaged in OOP and has not learned a full set.

 

Fortunately, as3 is still in the ECMA script family, which means that I can:

 

  1. Public class classfactory2 extends classfactory
  2. {
  3. Public Function classfactory2 (generator: class = NULL, initializer: function = NULL)
  4. {
  5. Super ();
  6. This. Generator = generator;
  7. This. initializer = initializer;
  8. }
  9. Public var initializer: function;
  10. Public override function newinstance ():*
  11. {
  12. VaR instance: Object = super. newinstance ();
  13. If (initializer! = NULL)
  14. {
  15. Initializer (instance );
  16. }
  17. Return instance;
  18. }
  19. }

When newinstance is used, an initialization letter named initializer is returned. Classfactory2 is provided for this function. Usage:

 

  1. VaR buttoncreator: classfactory2 = new classfactory2 (MX. Controls. Button,
  2. Function (button: MX. Controls. Button): void {
  3. Button. Label = 'command ';
  4. Button. addeventlistener (mouseevent. Click, function (EVT: mouseevent): void {
  5. Alert. Show ('hello ');
  6. });
  7. }
  8. );
  9. Testcolumn. itemrenderer = buttoncreator;

 

It succeeded !!

 

Next, let's take a further step. a simpler version:

 

  1. Import MX. Core. ifacloud;
  2. Public class simpleclassfactory implements ifacloud
  3. {
  4. Private var Creator: function = NULL;
  5. Public Function simpleclassfactory (Creator: function)
  6. {
  7. This. creator = creator;
  8. }
  9. Public Function newinstance ():*{
  10. Return creator ();
  11. }
  12. }

 

The method used for logging is not enough.

 

Although as3 was foolish enough to rely on OOP, fortunately, it still retains some features of FP. At the moment of the relationship, FP stood up and saved the revolution and the stupid as3.

 

Recall that in the rhino Runtime Engine of JDK 6, the new thread (function () {...}) method is even supported. Rhino automatically creates internal classes to implement the runnable interface.

 

In comparison, the design of as3 still needs to improve the recognition of OOP and FP. At the ECMA script conference last time, Adobe wanted to set the as3 statement as a standard. Fortunately, the ECMA Committee did not hear anything from him.

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.