Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache flex Flex/AS3-使用字符串动态调用函数?_Apache Flex_Actionscript 3 - Fatal编程技术网

Apache flex Flex/AS3-使用字符串动态调用函数?

Apache flex Flex/AS3-使用字符串动态调用函数?,apache-flex,actionscript-3,Apache Flex,Actionscript 3,是否可以使用字符串值作为函数名调用AS3中的函数,例如 var functionName:String = "getDetails"; var instance1:MyObject = new MyObject(); instance1.functionName(); // I know this is so wrong, but it gets the point accross:) 更新 @Taskinoor在访问函数时给出的答案是正确的: instance1[functionName

是否可以使用字符串值作为函数名调用AS3中的函数,例如

var functionName:String = "getDetails";

var instance1:MyObject = new MyObject();

instance1.functionName(); // I know this is so wrong, but it gets the point accross:)
更新

@Taskinoor在访问函数时给出的答案是正确的:

instance1[functionName]();
要访问我们将使用的财产:

instance1[propertyName]
instance1[functionName](); 查看一些详细信息。

例如,当您不知道对象是否有这样的方法时,您可以使用function.apply()或function.call()方法

var functionName:String = "getDetails";
var instance1:MyObject = new MyObject();
var function:Function = instance1[functionName]
if (function)
    function.call(instance1, yourArguments)

我创建了以下用于调用函数的包装器。您可以通过其名称或实际函数调用它。我试图使这些尽可能容易出错

以下函数将函数名转换为给定范围的相应函数

public static function parseFunc(func:*, scope:Object):Function {
    if (func is String && scope && scope.hasOwnProperty(funcName)) {
        func = scope[func] as Function;
    }
    return func is Function ? func : null;
}
呼叫 签名:
调用(func:*,作用域:Object,…args):*

public static function call(func:*, scope:Object, ...args):* {
    func = parseFunc(func, scope);
    if (func) {
        switch (args.length) {
            case 0:
                return func.call(scope);
            case 1:
                return func.call(scope, args[0]);
            case 2:
                return func.call(scope, args[0], args[1]);
            case 3:
                return func.call(scope, args[0], args[1], args[2]);
            // Continue...
        }
    }
    return null;
}
public static function apply(func:*, scope:Object, argArray:*=null):* {
    func = parseFunc(func, scope);
    return func != null ? func.apply(scope, argArray) : null;
}
申请 签名:
apply(func:*,作用域:Object,argArray:*=null):*

public static function call(func:*, scope:Object, ...args):* {
    func = parseFunc(func, scope);
    if (func) {
        switch (args.length) {
            case 0:
                return func.call(scope);
            case 1:
                return func.call(scope, args[0]);
            case 2:
                return func.call(scope, args[0], args[1]);
            case 3:
                return func.call(scope, args[0], args[1], args[2]);
            // Continue...
        }
    }
    return null;
}
public static function apply(func:*, scope:Object, argArray:*=null):* {
    func = parseFunc(func, scope);
    return func != null ? func.apply(scope, argArray) : null;
}

笔记 呼叫 由于
…args
参数.slice(2)
都是数组,因此需要使用该开关。您需要使用变量参数调用
Function.call()

申请
内置函数()为
argArray
使用非类型参数。我只是在背负这一点。

好的一个,很难用谷歌搜索答案,这就是我所追求的。只是背负这一点,你可以使用
中的
关键字:
if(functionName in instance1)
“提问,得到答案。你有一个地狱般的系统。”-Seth Rogan,观察并报告。可以是SO的moto:)作为旁注,这里的优点是apply()允许您在数组中传递参数,而不是内联指定参数。@Luis:请参阅下面的我的。您仍然可以通过传递
…args
来内联指定它们。您只需确保开关可以处理参数的长度。此外,是否应该将apply()argsArray参数键入数组?我添加了一个注释部分来回答您的一些问题。这给了我一个著名的问题成就:-)10k+视图。这个网站为我节省了很多时间——比stackoverflow之前的日子少了很多程序员的愤怒。这是一个伟大的社区,有着伟大的制度