I’m writing some Actionscript3 code that attempts to apply a method to an object that is determined at runtime. The AS3 documentation for Function.apply and Function.call both indicate that the first argument to those functions is the object which will be used as the ‘this’ value when the function is executed.
However, I have found that in all cases when the function being executed is a method, the first parameter to apply/call is not used, and ‘this’ always refers to the original object to which that method was bound. Here is some example code and its output:
package
{
import flash.display.Sprite;
public class FunctionApplyTest extends Sprite
{
public function FunctionApplyTest()
{
var objA:MyObj = new MyObj("A");
var objB:MyObj = new MyObj("B");
objA.sayName();
objB.sayName();
objA.sayName.apply(objB, []);
objA.sayName.call(objB);
}
}
}
internal class MyObj
{
private var _name:String;
public function MyObj(name:String)
{
_name = name;
}
public function sayName():void
{
trace(_name);
}
}
Output:
A
B
A
A
A minor modification to the above code to create an in-line anonymous function which refers to ‘this’ shows that the correct behavior occurs when the function being applied/called is not a bound method.
Am I using apply/call incorrect when I attempt to use it on a method? The AS3 documentation specifically provides code for this case, however:
myObject.myMethod.call(myOtherObject, 1, 2, 3);
If this is indeed broken, is there any work-around besides making the target methods into functions (which would be quite ugly, in my opinion)?
Its not a “bug”, but the documentation for
callandapplyis very misleading and doesn’t do a good job at all of explaining whats going on. So here is an explaination of what is happening.Methodsare different fromFunctionsin ActionScript.Methodsare defined as a part of a class defintion, and methods are always bound to that instance. See the Methods second of this link. To quote from there:So when you make a
newinstance ofMyObj, all of its methods are bound to that instance. Which is why when you try to usecallorapply, you aren’t seeingthisgetting overridden. See the section on Bound Methods for details.See, this document for an explanation of the traits object, which actionscript uses to resolve methods and used for performance reasons behind the scenes is probably to blame. That or class methods are just syntactic sugar for the following ECMAScript pattern:
Then:
or even more interesting:
Vs:
Notice that
boundWorkwill always get executed in the context of the instance it belongs to, no matter what you pass in forthiswithcallorapply. Which, in ActionScript, this behavior is exactly why class methods are bound to their instance. So no matter where they are used, they still point at the instance they came from (which makes the actionscript event model a little more “sane”). Once you understand this, then a work-around should become obvious.For places where you want to do some magic, avoid the ActionScript 3 based hard-bound methods in favor of prototype functions.
For example, consider the following ActionScript code:
Notice the declaration difference between
sayNameandpSayName.sayNamewill always be bound to the instance it was created for.pSayNameis a function that is available to instances ofMyObjbut is not bound to a particular instance of it.The documentation for
callandapplyare technically correct, as long as you are talking about prototypicalfunctionsand not classmethods, which I don’t think it mentions at all.