Entry
when passing a variable number of parameters you have explained the use of the code 'arg0 = arg0 == void 0 ? 0 : arg0'. Please explain what it does?
Dec 22nd, 2000 22:31
Juergen Thelen, Peter Parker,
Hi Peter,
To understand the block
function functionName (arg0, arg1, arg2, arg3) {
arg0 = arg0 == void 0 ? 0 : arg0;
..
}
you first need to know the functionality of the special operators '?:'
and 'void'.
The special operator '?:' is just a shortcut form of an if-else-block,
so instead of writing:
function functionName (arg0, arg1, arg2, arg3) {
arg0 = arg0 == void 0 ? 0 : arg0;
..
}
I could also write:
function functionName (arg0, arg1, arg2, arg3) {
if (arg0 == void 0)
arg0 = 0
else
arg0 = arg0;
..
}
The void operators task is to evaluate the expression following the
void keyword. The usage of parantheses surrounding the expression to be
evaluated is optional, but for now I'll put them back in to emphasize
the relationship:
function functionName (arg0, arg1, arg2, arg3) {
if ( arg0 == void(0) )
arg0 = 0
else
arg0 = arg0;
..
}
The particularity of void is, that it evaluates the given expression,
but does not return a value as a result. In other words void(0) simply
stands for 'undefined' here, so I could pseudocode:
function functionName (arg0, arg1, arg2, arg3) {
if ( arg0 == 'undefined' )
arg0 = 0
else
arg0 = arg0;
..
}
Now imagine several cases of calling this function:
Case #1 functionName();
Case #2 functionName("a", "b");
Case #3 functionName(null, "b");
Case #1: the function is called without any parameters, hence arg0,
arg1, arg2 and arg3 will have no values upon function entry. This will
be detected by the if-block and arg0 will be set to 0 (arg0 = 0).
Case #2: the function is called with two defined parameters, hence upon
function entry arg0 will hold "a", and arg1 will hold "b". This time
arg0 is not undefined, so arg0 remains unchanged (arg0 = arg0 = "a").
Case #3: the function is called with the special keyword 'null' and the
defined parameter "b". Usage of the special keyword 'null' results in
assigning a null value (meaning no value, not a value of zero!) to the
first parameter, so arg0 will have no value, and arg1 will be "b" upon
function entry. The if-block will detect that arg0 has no value, and
like in Case#1 set arg0 to 0. The null keyword comes handy if some
argments can or must be skipped, but other arguments have to be set.
Hope this helps.
Juergen