简体   繁体   English

Flex / AS3 - 使用String动态调用函数?

[英]Flex/AS3 - calling a function dynamically using a String?

Is it possible to call a function in AS3 using a string value as the function name eg 是否可以使用字符串值作为函数名称来调用AS3中的函数,例如

var functionName:String = "getDetails";

var instance1:MyObject = new MyObject();

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

UPDATE UPDATE

The answer from @Taskinoor on accessing a function is correct: @Taskinoor在访问函数时的答案是正确的:

instance1[functionName]();

And to access a property we would use: 要访问我们将使用的属性:

instance1[propertyName]
instance1[functionName]();

Check this for some details. 检查一下这些细节。

You may use function.apply() or function.call() methods instead in the case when you dont know whether object has such method for instance. 在您不知道对象是否具有此类方法的情况下,您可以使用function.apply()或function.call()方法。

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

I have created the following wrappers for calling a function. 我已经为调用函数创建了以下包装器。 You can call it by its name or by the actual function. 您可以通过其名称或实际功能来调用它。 I tried to make these as error-prone as possible. 我试图让这些尽可能容易出错。

The following function converts a function name to the corresponding function given the scope. 以下函数将函数名称转换为给定范围的相应函数。

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;
}

Call 呼叫

Signature : call(func:*,scope:Object,...args):* 签名call(func:*,scope: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;
}

Apply 应用

Signature : apply(func:*,scope:Object,argArray:*=null):* 签名apply(func:*,scope:Object,argArray:*=null):*

public static function apply(func:*, scope:Object, argArray:*=null):* {
    func = parseFunc(func, scope);
    return func != null ? func.apply(scope, argArray) : null;
}

Notes 笔记

Call 呼叫

The switch is needed, because both ...args and arguments.slice(2) are Arrays. 需要切换,因为...argsarguments.slice(2)都是数组。 You need to call Function.call() with variable arguments. 您需要使用可变参数调用Function.call()

Apply 应用

The built-in function ( apply(thisArg:*, argArray:*):* ) uses a non-typed argument for the argArray . 内置函数( apply(thisArg:*, argArray:*):* )使用argArray的非类型参数。 I am just piggy-backing off of this. 我只是背负着这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM