Scoping is mostly a description of what the this keyword refers to at any given point in your application. In an <mx:Script> tag in an MXML file, the this keyword always refers to the current scope. In the main application file, the file that contains the <mx:Application> tag, the current scope is the Application object; therefore, the this keyword refers to the Application object.
In an MXML component, Flex executes in the context of the custom component. The current scope is defined by the root tag of the file. So, the this keyword refers not to the Application object, but to the object defined by the root tag of the MXML file.
For more information on scoping, see Using ActionScript.
The root tag of an MXML component cannot contain an id property. Therefore, if you refer to the object defined by the root tag in the body of the component, you must use the this keyword, as the following example shows:
<?xml version="1.0"?>
<!-- mxml/myComponents/StateComboBoxThis.mxml -->
<mx:ComboBox xmlns:mx="http://www.adobe.com/2006/mxml"
close="handleCloseEvent(event);">
<mx:Script>
<![CDATA[
import flash.events.Event;
// Define a property to hold the current index.
public var stateIndex:Number;
private function handleCloseEvent(eventObj:Event):void {
stateIndex = this.selectedIndex;
}
]]>
</mx:Script>
<mx:dataProvider>
<mx:String>AK</mx:String>
<mx:String>AL</mx:String>
</mx:dataProvider>
</mx:ComboBox>
This example defines an event listener for the ComboBox control that updates the stateIndex property when the ComboBox control closes.