AS3, knowing how many arguments the function takes

Is there a way to find out how many arguments a function instance can take in Flash? It would also be very helpful to know if these arguments are optional or not.

For instance:

public function foo() : void //would have 0 arguments public function bar(arg1 : Boolean, arg2 : int) : void //would have 2 arguments public function jad(arg1 : Boolean, arg2 : int = 0) : void //would have 2 arguments with 1 being optional 

thanks

+4
source share
1 answer

Yes, there is: use the Function.length property. I just checked the docs : it doesn't seem to be mentioned there.

 trace(foo.length); //0 trace(bar.length); //2 trace(jad.length); //2 

Note that there are no curly braces () after the function name. You need a reference to the Function object; adding curly braces will execute the function.

I do not know how to find out that one of the arguments is optional.

EDIT

How about ... relaxation options ?

 function foo(...rest) {} function bar(parameter0, parameter1, ...rest) {} trace(foo.length); //0 trace(bar.length); //2 

This makes sense as there is no way to know how many arguments will be passed. Please note: inside the function body, you can know exactly how many arguments were passed, for example:

 function foo(...rest) { trace(rest.length); } 

Thanks to @felipemaia for pointing this out.

+13
source

Source: https://habr.com/ru/post/1382880/


All Articles