一、如果需要移除监听事件中的委派切记不可以使用匿名类:
myButton.addEventListener("click",mx.utils.Delegate.create(this,onClick));
这样使用匿名类来传入,无法移除
myButton.removeEventListener("click",mx.utils.Delegate.create(this.onClick));
正确做法是保存引用
var myDelegate=mx.utils.Delegate.create(this.onClick);
myButton.addEventListener("click",myDelegate);
myButton.removeEventListener("click",myDelegate);
二、内置类事件的委派可以使用Delegate
我们一般的做法是增加owner来指向this路径
例:
var myCam=Camera.get();
Object(myCam).owner=this;
myCam.onStatus=function(info){
this.owner.onCamStatus;
}
可以写成
var myCam=Camera.get();
myCam.onStauts=mx.utils.Delegate.create(this,onCamStatus);
在onCamStauts中的this指向是正确的!
myButton.addEventListener("click",mx.utils.Delegate.create(this,onClick));
这样使用匿名类来传入,无法移除
myButton.removeEventListener("click",mx.utils.Delegate.create(this.onClick));
正确做法是保存引用
var myDelegate=mx.utils.Delegate.create(this.onClick);
myButton.addEventListener("click",myDelegate);
myButton.removeEventListener("click",myDelegate);
二、内置类事件的委派可以使用Delegate
我们一般的做法是增加owner来指向this路径
例:
var myCam=Camera.get();
Object(myCam).owner=this;
myCam.onStatus=function(info){
this.owner.onCamStatus;
}
可以写成
var myCam=Camera.get();
myCam.onStauts=mx.utils.Delegate.create(this,onCamStatus);
在onCamStauts中的this指向是正确的!
回复Comments
作者:
{commentrecontent}