Function.apply not using thisArg parameter

后端 未结 4 1068
遇见更好的自我
遇见更好的自我 2020-12-16 20:48

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 b

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-16 21:05

    Wow this is very surprising O.o

    Tested it on my side as well and tried to pass in parameters as well and in all cases, the passed thisArg doesn't seem to be used at all (clearly seems like a bug to me).

    I had to use something similar but had the additional constraint of needing to access the method without creating an instance of the object (which is possible in other languages but not in AS3 >.<). So I ended up creating static functions and passed my own "thisArg" instead.

    So making static functions instead is a possible workaround :

    static public function SayName(thisArg : MyObj) : void
    {
        trace(thisArg._name);
    }
    

    Not the greatest since you'll probably end up doubling code like this >.<

    Alternatively, if the methods are public, you can save the function's name instead of the function and access it's method by doing something like :

    var funcName : String = "sayName";
    objB[funcName].apply(null, []);
    objB[funcName].call(null);
    

    However, this is limited depending on the scope of your method (public methods can be used like this from everywhere, internal methods within the same package as your class and private methods from inside the class only). So it is more limiting than using the actual Function instance of your method which can be used from anywhere.

    This seems like a pretty nasty bug O.o I hope someone else has a better solution to this.

提交回复
热议问题