ActionScript2 delegate research (middle)

xiaoxiao2021-03-06  94

First look at the original class of delegate, it is written by Mike Chambers.

Class com.macromedia.mesh.events.EventProxy

{

Private var receiverobj: Object;

Private var funcname: String;

/ *

Constructor:

-receiverobj: The Receiverobj in Which The Event Will Be Called

-The function name to be called in response to the eve

* /

Function EventProxy (ReceiverObj: Object, FuncName: String)

{

THIS.RECEIVEROBJ = ReceiverObj;

THIS.FUNCNAME = FuncName;

}

// this is Called Before The Event Is Broadcast by The Components

Private function handleevent (eventobj: object): Void

{

// if not function name HAS been Defined

IF (FuncName == Undefined)

{

//pass the call to the event name method

Receiverobj [EventObj.Type] (EventObj);

}

Else

{

// pass the call to the specified method name

ReceiverObj [FuncName] (EventObj);

}

}

}

Obviously it can only be used on the V2 component, because only the component is only the EventTypenAme event, the HandleEvent event of the ProxyInstance (instance of the EventProxy class) is automatically performed when EventTypename event occurs. Other classes of other non-V2 components such as XML, MovieClip, or objects of their own define classes cannot perform this agent. I think Mike Chambers is written by the event of the component. Of course, this can also be seen on the article about Delegate, so this will mislead readers. Make readers think that Delegate is only for component services.

The Delegate class issued by Macromedia is improved by the EventProxy class of Mike Chambers. There are two different points with EventProxy. First, DELEGATE.CREATE returns a function, not objects like EventProxy; secondly, DELEGATE is more than just components, but also an instance of other classes. These can be seen in the above. Now study the Delegate code:

/ **

The Delegate Class Creates A Function Wrapper To Let You Run A Function In The Context

Original Object, Rather Than in The Context of The Second Object, When You Pass A

Function from One Object to Another.

* /

Class mx.utils.delegate Extends Object

{

/ **

Creates a Functions Wrapper for the Original Function So That It Runs

In the provided context.

@Parameter Obj Context in which to run the function. @ Paramater Func Function To Run.

* /

Static Function Create (Obj: Object, Func: Function): Function

{

VAR f = function ()

{

Var Target = arguments.callee.target;

Var func = arguments.callee.func;

Return Func.Apply (Target, Arguments);

}

f.target = OBJ;

F.Func = func;

Return F;

}

Function delegate (f: function)

{

Func = f;

}

PRIVATE VAR FUNC: FUNCTION;

Function CreateDelegate (OBJ: Object): Function

{

Return Create (Obj, FUNC);

}

}

The most important CREATE method, which uses a lazy initialization method. We divide Create's code into two paragraphs, so you can see more obvious:

Static Function Create (Obj: Object, Func: Function): Function

{

VAR f: function = proxy;

f.target = OBJ;

F.Func = func;

Return F;

}

Static function proxy ()

{

Var Target = arguments.callee.target;

Var func = arguments.callee.func;

Return Func.Apply (Target, Arguments);

}

First define a Function instance F and pass:

Var Target = arguments.callee.target;

Var func = arguments.callee.func;

These two sentences make Target to keep a reference with f.target (already arguments.callee.target). The f.Target has not been assigned this at this time, and F.Func is the same. The value is assigned until these two sentences appear.

f.target = OBJ;

F.Func = func;

We can use a simple trial to verify:

VAR A: Object = new object ();

A.c = new object ();

// b Keep a reference to A.c, but at this time A.c does not assign

// If A.C is a simple type, such as 10, "ABC" is not

VAR B = a.c;

A.c.d = 100

// Output is 100.

Trace (B.D);

Create returns a proxy function f, which is not executed at CREATE. Until this function is called when it is called. The components are also called this function after the Click event occurs.

The next article will continue to pay attention to Delgate's implementation, and how to discuss how Macromedia has not realized multicast commission.

转载请注明原文地址:https://www.9cbs.com/read-104278.html

New Post(0)