User Guide

126 ActionScript language elements
Example
Create an ActionsScript file named ApplyThis.as and then enter the following code:
class ApplyThis {
var str:String = "Defined in ApplyThis.as";
function conctStr(x:String):String {
return x+x;
}
function addStr():String {
return str;
}
}
Then, in a FLA or a separate ActionScript file, add the following code
var obj:ApplyThis = new ApplyThis();
var abj:ApplyThis = new ApplyThis();
abj.str = "defined in FLA or AS";
trace(obj.addStr.call(abj, null)); //output: defined in FLA or AS
trace(obj.addStr.call(this, null)); //output: undefined
trace(obj.addStr.call(obj, null)); //output: Defined in applyThis.as
Similarly, to call a function defined in a dynamic class, you must use this to invoke the
function in the proper scope:
// incorrect version of Simple.as
/*
dynamic class Simple {
function callfunc() {
trace(func());
}
}
*/
// correct version of Simple.as
dynamic class simple {
function callfunc() {
trace(this.func());
}
}
Inside the FLA or a separate ActionScript file, add the following code:
var obj:Simple = new Simple();
obj.num = 0;
obj.func = function() {
return true;
};
obj.callfunc();
// output: true
The above code works when you use this in the callfunc() method. However you would get a
syntax error if you used the incorrect version of Simple.as, which was commented out in the
above example.