How can we dynamically call a function. I have tried below code:
public function checkFunc() : void
{
Alert.show("inside function");
}
public var myfunc:String = "checkFunc";
public var newFunc:Function=Function(myfunc);
newFunc();
But it gives error:
Call to a possibly undefined method newFunc.
In place of newFunc(), I tried calling it as this[newFunc](), but that throws error:
The this keyword can not be used in static methods. It can only be used in instance methods, function closures, and global code
Any help in calling the function dynamically?
Functions work the same way properties to, you can assign them the same way you assign variables, meaning that all the funky square bracket tricks work for them too.
public function checkFunc() : void
{
Alert.show("inside function");
}
public var myfunc:String = "checkFunc";
public var newFunc:Function = this[myfunc];
newFunc();
From taskinoor's answer to this question:
instance1[functionName]();Check this for some details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With