콜백 함수 내에서 클래스의 메서드를 호출 할 때 this
개체를 사용할 수 없습니다.
자바 스크립트에서 메소드를 호출하려면 that
을 선언하고 this
을 that
에 할당하고 that
을 콜백 내에 사용하여 this
메서드를 호출합니다.
actionscript에서 javascript와 동일한 방식으로 수행해야합니까?
다음 코드는 that
을 사용하여 콜백 내부의 메서드를 호출하는 예제입니다.
더 간단한 방법으로 actionscript에 있습니까?
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');
}
}
왜 'this'를 그냥 사용하지 않으시겠습니까? – OXMO456
@ OXMO456 대단히 감사합니다. 'this'을 생략하면 효과가있었습니다. 그것은 매우 간단한 해결책입니다. –