当你调用一个类的方法的回调函数里面,你不能使用这
对象。
要调用该方法,在JavaScript中,我宣布是
变量,分配这
到的
,并使用是
回调内部调用的方法这
。
在动作,做我必须做同样的方式,因为我做的JavaScript?
下面code为例子,使用是
来调用内部的回调方法。
是否有动作更简单的方式?
C类{
私人VAR是:C;
函数C(){
该=这个
}
公共函数f1():无效{
变种SP:雪碧=新雪碧;
sp.addEventListener(MouseEvent.CLICK,函数():无效{
this.f2(); //这不起作用
that.f2(); //的作品
});
}
公共函数f2():无效{
跟踪('你好');
}
}
解决方案
下面是另一种方式来做到这一点:
包some.package {
类SomeSprite扩展Sprite {
公共函数f1():无效
{
VAR sprite1:雪碧=新雪碧;
sprite1.addEventListener(MouseEvent.CLICK,
(新关闭(F2,这一点,[你好],sprite1).handler));
VAR sprite2:雪碧=新雪碧;
sprite2.addEventListener(MouseEvent.CLICK,
(新关闭(F2,这一点,[再见],sprite2).handler));
}
私有函数f2(消息:字符串):无效
{
迹(讯息);
}
}
}
一流的封闭{
公共变种回调:函数;
公共变种thisObj:对象;
公共变种params:一个阵列;
公共变种调度员:IEventDispatcher,请;
公共变种cleanAfterCallback:布尔=真;
函数闭包(回调:功能,thisObj:对象,
params:一个数组,调度员:IEventDispatcher,请)
{
this.callback =回调;
this.thisObj = thisObj;
this.params = PARAMS;
this.dispatcher =调度;
}
公共职能处理器(五:事件):无效
{
callback.apply(thisObj,则params);
如果(cleanAfterCallback)
dispatcher.removeEventListener(e.type,事件处理程序)
}
}
When you call a method of a class inside a callback function, you can not use this
object.
To call the method, in javascript, I declare that
variable, assign this
to that
, and use that
inside the callback to call the method of this
.
In actionscript, do I have to do the same way as I do in javascript?
The following code is the example to use that
to call a method inside callback.
Are there more simple way in actionscript?
class C {
private var that:C;
function C() {
that = this
}
public function f1():void {
var sp:Sprite = new Sprite;
sp.addEventListener(MouseEvent.CLICK, function():void {
this.f2(); // this doesn't work
that.f2(); // that works
});
}
public function f2():void {
trace('hello');
}
}
解决方案
Here's another way to do that:
package some.package {
class SomeSprite extends Sprite {
public function f1():void
{
var sprite1:Sprite = new Sprite;
sprite1.addEventListener(MouseEvent.CLICK,
(new Closure(f2, this, ["Hello"], sprite1).handler));
var sprite2:Sprite = new Sprite;
sprite2.addEventListener(MouseEvent.CLICK,
(new Closure(f2, this, ["Bye Bye"], sprite2).handler));
}
private function f2(message:String):void
{
trace(message);
}
}
}
class Closure {
public var callback:Function;
public var thisObj:Object;
public var params:Array;
public var dispatcher:IEventDispatcher;
public var cleanAfterCallback:Boolean = true;
function Closure(callback:Function, thisObj:Object,
params:Array, dispatcher:IEventDispatcher)
{
this.callback = callback;
this.thisObj = thisObj;
this.params = params;
this.dispatcher = dispatcher;
}
public function handler(e:Event):void
{
callback.apply(thisObj, params);
if (cleanAfterCallback)
dispatcher.removeEventListener(e.type, eventHandler)
}
}
上一篇:表级事件时的数据是MSACCESS表格上改变了?表格、事件、改变了、数据
下一篇:什么是运行在2003年和2007年Microsoft Access数据库的含义是什么?含义、数据库、Access、Microsoft