Scope of the this keyword
The
keyword refers to the object in the currently executing scope. Depending on what
this
type of event handler technique you use,
Within an event handler or event listener function
event handler or event listener method. For example, in the following code,
:
my_mc
// onPress() event handler attached to main timeline:
my_mc.onPress = function () {
trace(this); // _level0.my_mc
}
Within an on() handler attached to a movie clip
handler is attached, as shown in the following code:
on()
// Attached to movie clip named my_mc on main timeline
on (press) {
trace(this); // _level0.my_mc
}
Within an on() handler attached to a button
button, as shown in the following code:
// Attached to button on main timeline
on (press) {
trace(this); // _level0
}
Using the Delegate class
The Delegate class lets you run a function in a specific scope. This class is provided so that
you can dispatch the same event to two different functions (see "Delegating events to
functions" in Using Components), and so that you can call functions within the scope of the
containing class.
When you pass a function as a parameter to
function is invoked in the scope of the broadcaster component instance, not the object in
which it is declared (see "Delegating the scope of a function" in Using Components). You can
use
Delegate.create()
The following example shows three methods of listening for events for a Button component
instance. Each way that you add event listeners to a Button component instance results in the
event being dispatched in a different scope.
this
,
EventDispatcher.addEventListener()
to call the function within the scope of the declaring object.
can refer to different objects.
,
refers to the object that defines the
this
,
refers to the movie clip to which the
this
refers to the timeline that contains the
this
Using the Delegate class
refers to
this
, the
347
Need help?
Do you have a question about the FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH and is the answer not in the manual?