Adobe FLEX 2-PROGRAMMING ACTIONSCRIPT 3.0 Manual page 360

Programming actionscript 3.0
Table of Contents

Advertisement

trace("the this keyword refers to: " + this);
}
}
When a user interacts with the resulting SWF file by clicking on the red square, Flash Player
generates the following trace output:
clickHandler detected an event of type: click
the this keyword refers to: [object ChildSprite]
Note that the
keyword refers to the ChildSprite instance named
this
in behavior from ActionScript 2.0. If you used components in ActionScript 2.0, you may
remember that when a class method was passed in to
UIEventDispatcher.addEventListener()
component that broadcast the event instead of the class in which the listener method was
defined. In other words, if you used this technique in ActionScript 2.0, the
would refer to the component broadcasting the event instead of the ChildSprite instance.
This was a significant issue for some programmers because it meant that they could not access
other methods and properties of the class containing the listener method. As a workaround,
ActionScript 2.0 programmers could use the
the listener method. This is no longer necessary, however, because ActionScript 3.0 creates a
bound method when
the ChildSprite instance named
and properties of the ChildSprite class.
Event listener that should not be used
There is a third technique in which you create a generic object with a property that points to
a dynamically assigned listener function, but it is not recommended. It is discussed here
because it was commonly used in ActionScript 2.0, but should not be used in ActionScript
3.0. This technique is not recommended because the
object instead of your listener object.
The following example is identical to the previous ClickExample class example, except that
the listener function is defined as part of a generic object named
package
{
import flash.display.Sprite;
public class ClickExample extends Sprite
{
public function ClickExample()
{
var child:ChildSprite = new ChildSprite();
addChild(child);
360
Handling Events
addEventListener()
, and the programmer has access to the other methods
child
, the scope of the method was bound to the
mx.util.Delegate
is called. As a result, the
keyword will refer to the global
this
myListenerObj
. This is a change
child
keyword
this
class to change the scope of
keyword refers to
this
:

Advertisement

Table of Contents
loading

This manual is also suitable for:

Flex

Table of Contents