Apache flex 动态调用函数
我们如何动态调用函数。我尝试了以下代码:Apache flex 动态调用函数,apache-flex,actionscript-3,actionscript,flex3,Apache Flex,Actionscript 3,Actionscript,Flex3,我们如何动态调用函数。我尝试了以下代码: public function checkFunc() : void { Alert.show("inside function"); } public var myfunc:String = "checkFunc"; public var newFunc:Function=Function(myfunc); newFunc(); 但它给出了错误: 调用可能未定义的方法 纽芬克 我尝试将其称为this[newFunc](),而不是newFunc()
public function checkFunc() : void
{
Alert.show("inside function");
}
public var myfunc:String = "checkFunc";
public var newFunc:Function=Function(myfunc);
newFunc();
但它给出了错误:
调用可能未定义的方法
纽芬克
我尝试将其称为this[newFunc]()
,而不是newFunc()
,但这会引发错误:
此关键字不能在中使用
静态方法。它只能用于
实例方法,函数闭包,
和全局代码
动态调用函数有什么帮助吗?从到:
instance1[functionName]();
查看一些详细信息
flash中的函数是对象,因此其功能与任何对象一样。AS3 api显示函数有一个call()方法。您的代码非常接近:
// Get your functions
var func : Function = someFunction;
// call() has some parameters to achieve varying types of function calling and params
// I typically have found myself using call( null, args );
func.call( null ); // Calls a function
func.call( null, param1, param2 ); // Calls a function with parameters
代码未经测试,但应该可以工作
package {
public class SomeClass{
public function SomeClass( ):void{
}
public function someFunc( val:String ):void{
trace(val);
}
public function someOtherFunc( ):void{
this['someFunc']('this string is passed from inside the class');
}
}
}
// usage
var someClass:SomeClass = new SomeClass( );
someClass['someFunc']('this string is passed as a parameter');
someClass.someOtherFunc();
// mxml example
// Again untested code but, you should be able to cut and paste this example.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="someOtherFunc( )" >
<mx:Script>
<![CDATA[
public function someFunc( val:String ):void{
trace(val);
this.theLabel.text = val
}
public function someOtherFunc( ):void{
// this is where call the function using a string
this['someFunc']('this string is passed from inside the class');
}
]]>
</mx:Script>
<mx:Label id="theLabel" />
</mx:Application>
包{
公共类{
公共函数SomeClass():void{
}
公共函数someFunc(val:String):void{
微量元素(val);
}
公共函数someOtherFunc():void{
this['someFunc']('this string是从类内部传递的');
}
}
}
//用法
var someClass:someClass=newsomeclass();
someClass['someFunc']('this string作为参数传递');
someClass.someOtherFunc();
//mxml示例
//同样是未经测试的代码,但是,您应该能够剪切并粘贴此示例。
函数与属性的工作方式相同,您可以与分配变量的方式相同,这意味着所有时髦的方括号技巧也适用于它们
public function checkFunc() : void
{
Alert.show("inside function");
}
public var myfunc:String = "checkFunc";
public var newFunc:Function = this[myfunc];
newFunc();
默认情况下,对象的第一个参数是函数所属的实例(即:“this”)oops我是说函数“call”的第一个参数是instance@Ben:它给出了相同的错误“访问未定义的属性newFunc”@sandy这不是您要查找的。这只是通过变量引用函数,但您仍然需要动态获取函数,这并没有显示如何操作。@阿斯曼:好的..den dat是如何完成的。。?在您发布的答案中,我们在另一个包中有函数。但对我来说,函数及其用法都在同一个mxml文件的操作脚本中。这个链接中给出的任何内容都是我在上面代码中所做的……但它给出了错误:(@sandy是静态类中包含的方法吗?可能是
public function checkFunc() : void
{
Alert.show("inside function");
}
public var myfunc:String = "checkFunc";
public var newFunc:Function = this[myfunc];
newFunc();