Why is overload not supported in ActionScript?

The script action is designed based on object-oriented programming, but why doesn't it support function overloading?

Does Flex support overload?

If yes, please explain briefly with a real example.

+5
source share
5 answers

As you say, the overload function is not supported in Action Script (and therefore not even in Flex).

But functions can have default parameters, for example here:

public function DoSomething(a:String='', b:SomeObject=null, c:Number=0):void

DoSomething can be called up in 4 different ways:

DoSomething()
DoSomething('aString')
DoSomething('aString', anObject)
DoSomething('aString', anObject, 123)

, Action Script ECMA Script. , , , . ( )

ECMA-262 ( ECMAScript) 13 (. 83 PDF) , ​​,

function Identifier(arg0, arg1) {
    // body
}

Identifier, Function, :

new Function(arg0, arg1, body)

, ,

+22

, , . , .

lk, , . :

public function overloaded(mandatory1: Type, mandatory2: Type, ...rest): *;

, , . , , .

+9

- -.

public function doSomething(...args):*{
    if(args.length==1){
        if(args[0] is String){
            return args[0] as String;
        }
        if(args[0] is Number){
            return args[0] as Number;
        }
    }
    if(args.length==2){
        if(args[0] is Number && args[1] is Number){
            return args[0]+args[1];
        }
    }

}
+1

, , , .

, , - , , / Adobe .

+1

Probably because ActionScript searches for functions by function name at run time, rather than storing them by name and parameters at compile time.

This function makes it easy to add and remove functions from dynamic objects, as well as the ability to get and call functions by name using object['functionName'](), but I believe that it is very difficult to overload without creating a mess of these functions.

+1
source

All Articles