Prev | Current Page 204 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

In
order to access this variable, you need to reference it through the class keyword this . However, the
this keyword in local variables is not allowed, as local functions are considered static, so a trick is
necessary. If the variables local to the containing variable are accessible, even in a class instance, the
value of this can be assigned to one such local variable and then referenced in the function:
class LocalFunctionVars
{
public static var myStaticVar1 : String;
public var myVar1 : String;
public static function main()
{
var l = new LocalFunctionVars();
}
public function new()
{
var myLocalVar1 : String;
var me = this;
(continued)
94
Part I: The Core Language
var myFunction = function()
{
var innerFunction : String;
innerFunction = ???haXe???;
myLocalVar1 = ???is???;
myStaticVar1 = ???really???;
// will now compile
me.myVar1 = ???amazing???;
}
}
}
Static variables and functions, and the this keyword are discussed in the next chapter. However, they
have been detailed here for the purposes of completion.


Pages:
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216