Actionscript2 Delegate研究(中)

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

首先看一下Delegate的原始类,它是由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 event

*/

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);

}

}

}

很明显它只能用在V2组件上面,因为只有组件在addEvenListenr(eventTypeName,proxyInstance )定义之后,发生了eventTypeName事件时自动执行proxyInstance(EventProxy类的实例)的handleEvent事件。其它非V2组件的其它类如XML,MovieClip或者自己定义类的对象都不能执行这个代理。我认为Mike Chambers在写这个类的时候,就是针对组件的事件来考虑的。当然这也可以在他的那篇关于Delegate的文章上看得出来,所以这会误导读者。使读者认为Delegate只为组件服务。

而Macromedia官方发布的Delegate类是由Mike Chambers的EventProxy类改进得出来的。与EventProxy有两点不同,首先Delegate.create返回的是一个函数,而非像EventProxy 实例化出来的对象;其次就是Delegate针对的不仅仅是组件,还包括其它类的实例。这些可以在上篇中看到。现在来研究一下Delegate的代码:

/**

The Delegate class creates a function wrapper to let you run a function in the context

of the 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);

}

}

最主要是的create方法,它采用的是懒惰初始化的方法。我们把create的代码分为两段,这样就看得更明显点儿:

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);

}

首先定义了一个Function实例f,然后通过:

var target = arguments.callee.target;

var func = arguments.callee.func;

这两句使得target与f.target(就是arguments.callee.target)保持一个引用。而f.target这时候还没有被赋值,f.func也是一样。直到这两句出现的时候,才被赋上值。

f.target = obj;

f.func = func;

我们可以用一个简单的试验来验证一下:

var a:Object = new Object();

a.c = new Object();

//b保持对a.c的引用,但这时a.c并没有赋值

//如果a.c是简单类型,比如10,”abc”就不行了

var b = a.c;

a.c.d = 100

//output出来的是100.

trace(b.d);

create返回了一个代理函数f,这个f 在create完的时候没有执行。直到这个函数被调用的时候才执行。而组件也就是在click事件发生后调用这个函数的,所以会执行。

下篇会继续关注delgate的实现,以及探讨Macromedia未实现的多播委托怎么来实现。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航