Variable arguments and default in Javascript, Actionscript and PHP

Who develops knows that one of the characteristics of functions ( function () ) is to have or not the input arguments. It can happen, sometimes you have to write a function that, based on input parameters, behaves differently (in OO programming this behavior are referred to as polymorphism). Variable parameters (varargs), already introduced at the time of the present by default in C and classical statement of the main :

1
int main (int argc, char * argv []);

But not to be confused with the default setting of a parameter, now fully supported in ActionScript 3.0.

Default arguments

For example in PHP you can define a function like this:

1
2
3
$a , $b = 5 ) { function bar ($ a, $ b = 5) {
/ / Todosomethink
}

the parameter $b is optional, ie if it is not passed during the call to the function will assume the default value of 5.

As anticipated in ActionScript 3.0 now fully supports this system:

ActionScript 3.0 introduces the ability to declare default parameter values ​​for a function. If a call to a function with default parameter values ​​omits a parameter with a default, it uses the value specified for that parameter in the function definition. All parameters with default values ​​must be positioned at the end of the parameter. The values ​​assigned as default values ​​must be compile-time constants. The existence of a default value for a parameter effectively makes that parameter an optional parameter, while a parameter without a default value is considered a required parameter.

The same goes for PHP: A parameter with a default value for optional wind.

In Actionscript 3.0, similar to the example discussed above in PHP, you can preset the input parameters in this way:

1
2
3
a : int , b : int = 5 ) : void { function bar (a: int , b: int = 5): void {
/ / Todosomethink
}

Even in this case the second parameter b automatically becomes optional.

In JavaScript, unfortunately, this characteristic is not - yet - supported. Although there are some "convoluted" techniques (see, for example, Default Arguments in JavaScript Functions ) to simulate or around the obstacle, the use of this technique in JavaScript is very limited.

Variable parameters

This technique, unlike its predecessor, allows to examine the content and the presence of the input parameters of a function. It is evident that this method can also be used to set the missing parameters. e func_get_arg() . In PHP, for example, we have three simple functions that operate on input parameters: func_num_args() , func_num_args() and func_get_arg() . These functions allow you to return respectively the number of arguments, return an array with all elements of input, retrieve a specific item of input. For example:

1
2
3
function bar () {
. func_num_args ( ) ; echo 'Number of arguments ='. func_num_args ();
}

Returns the number of arguments passed to the function pluto . In the example below, however, is seen as a function of its behavior may vary depending on the number (and also the type - polimofica) of its parameters. In this case, if we call pluto() with two or more parameters is displayed otherwise not.

1
2
3
4
function bar () {
func_num_args ( ) > 2 ) if ( func_num_args ()> 2)
. func_get_arg ( 1 ) ; // essendo in base 0 echo 'The second parameter is'. func_get_arg (1), / / being 0-based
}

This also allows you to "simulate" a set of default parameters, but since PHP supports native and still more convenient that set out above. But here's an example to clarify:

1
2
3
4
5
function bar () {
! isset ( func_get_arg ( 1 ) ) ) { if ( isset ( func_get_arg (1))) {
/ / The second parameter was not passed
}
}

or:

1
2
3
4
5
function bar () {
func_num_args ( ) < 2 ) { if ( func_num_args () <2) {
/ / The second parameter was not passed
}
}

In Actionscript and Javascript have a situational like this:

1
2
3
4
5
6
7
function bar () {
'Numero argomenti passati = ' + arguments . length ) ; trace ('Number of arguments =' + arguments . length);
/ / Print all the topics
var i : uint = 0 ; i < arguments . length ; i ++ ) { for (var i: uint = 0; i < arguments . length; i + +) {
arguments [ i ] ) ; trace ( arguments [i]);
}
}

Javascript:

1
2
3
4
5
6
7
8
9
function bar () {
'Numero argomenti passati = ' + arguments. length ) ; alert ('Number of arguments =' + arguments. length);
/ / Print all the topics
'' ; var o ='';
var i = 0 ; i < arguments. length ; i ++ ) { for (var i = 0; i <arguments. length; i + +) {
i ] + ' \n ' ; or + = arguments [i] + '\ n';
}
o ) ; alert (o);
}

Since ECMA, Actionscript and Javascript resemble a lot, using both an object arguments which then provides various information.

2 comments to "topics and default variables in Javascript, Actionscript and PHP"

  1. June 11, 2008 Topics variables in Actionscript 3.0 | Undolog.com :

    [...] Post topics and default variables in Javascript, Actionscript and PHP I was exposed using the techniques of parameter passing and default variables. Add, now, [...]

  2. January 5th, 2009 WordPress: wp_parse_args () | Undolog.com :

    [...] In WordPress functions and is useful when a function supports multiple paramteri. As stated in Subjects and default variables in Javascript, Actionscript and PHP, there is no need to pass parameters in the mode [...]

Leave a comment

XHTML TAG PERMIT: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL