User Guide

ActionScript coding standards 97
this.stopDrag();
};
For a class, you can write code in the following format:
class User {
private var m_username:String;
private var m_password:String;
function User(username:String, password:String) {
this.m_username = username;
this.m_password = password;
}
public function get username():String {
return this.m_username;
}
public function set username(username:String):Void {
this.m_username = username;
}
}
If you consistently add the this keyword in these situations, your ActionScript will be much
easier to read and understand.
Using scope in classes
When you port code to ActionScript 2.0 classes, you might have to change how you use the
this
keyword. For example, if you have a class method that uses a callback function (such as the
LoadVars classs
onLoad method), it can be difficult to know if the this keyword refers to the
class or the LoadVars object. In this situation, it might be necessary to create a pointer to the
current class, as the following example shows:
class Product {
private var m_products_xml:XML;
// constructor
// targetXml_string - contains the path to an XML file
function Product(targetXml_string:String) {
/* Create a local reference to the current class.
Even if you are within the XML's onLoad event handler, you
can reference the current class instead of only the XML packet. */
var thisObj:Product = this;
// Create a local variable, which is used to load the XML file.
var prod_xml:XML = new XML();
prod_xml.ignoreWhite = true;
prod_xml.onLoad = function(success:Boolean) {
if (success) {
/* If the XML successfully loads and parses,
set the class's m_products_xml variable to the parsed
XML document and call the init function. */
thisObj.m_products_xml = this;
thisObj.init();
} else {
/* There was an error loading the XML file. */
trace("error loading XML");
}
};