|
Articles | Arguments Array |
back
When using JavaScript functions it is not always clear
how many parameters will be passed by the calling JavaScript. For example I have
used a function called makeArray() in the past to create an array of
strings. The value of the strings being passed as parameters to the function. I
have also used this function several times within the same JavaScript to create
one, two or three different arrays, each with different numbers of parameters.
You can call a function with more arguments than it is
formally declared to accept by using the arguments array. You can use
arguments.length to determine the number of arguments passed to the
function, and then treat each argument by using the arguments array.
However you must refer to the function object by name,
e.g. the following JavaScript, which has been used elsewhere on the JavaScript 'No Content'
Web site creates two arrays called days and months using the makeArray()
and makeArray0() functions:
<script language="JavaScript"><!--
function makeArray() {
for (i = 0; i<makeArray.arguments.length; i++)
this[i + 1] = makeArray.arguments[i];
}
function makeArray0() {
for (i = 0; i<makeArray0.arguments.length; i++)
this[i] = makeArray0.arguments[i];
}
var days = new makeArray("Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday","Sunday");
var months = new makeArray0('January','February','March',
'April','May','June','July','August','September',
'October','November','December');
//--></script>
|
The difference between makeArray() and makeArray0(),
is that makeArray() populates the array from element 1 not
element 0 by using this[i + 1] instead of this[i].
It is also possible to use a mix of declared and
undeclared arguments, by declaring arguments in the function definition and then
using the arguments array to access the undeclared arguments.
For example, in the following JavaScript which outputs
the properties of the window.document object followed by the properties
of the navigator object, the show_property() function has one
declared argument called object.
The show_property() function then examines all
the arguments in the arguments[] array from position 1 (i.e.
skipping the declared argument in position 0) to the end of the arguments[]
array using arguments.length.
Again notice that the name of the function show_property
is used to access the arguments[] array.
<script language="JavaScript"><!--
function show_property(object) {
for (var i = 1; i < show_property.arguments.length; i++)
document.write(object+'.'+show_property.arguments[i]+
' = '+eval(object+'.'+show_property.arguments[i])+'<br>');
}
show_property('window.document','alinkColor','bgColor','cookie',
'domain','fgColor','lastModified','linkColor','referrer','title',
'URL','vlinkColor');
show_property('navigator','appCodeName','appName','appVersion',
'userAgent');
//--></script>
|
This JavaScript will produce the following output:
It is also possible to pass less arguments to a
function than is expected, for example the following show() function
expects four arguments, yet show('1st','2nd','3rd'); uses only the
first three arguments, the fourth argument is effectively NULL, whereas show('1st',null,null,'4th');
uses null to assign null values to arguments, to space out the first
and fourth arguments, otherwise the second argument would be passed the value 4th.
It is then a simple case of testing whether a variable is not null using if(variable_name)
before using it:
<script language="JavaScript"><!--
function show(first,second,third,fourth) {
document.write('Output:<br>');
if (first) document.write('First = ',first,'<br>');
if (second) document.write('Second = ',second,'<br>');
if (third) document.write('Third = ',third,'<br>');
if (fourth) document.write('Fourth = ',fourth,'<br>');
}
show('1st','2nd','3rd');
show('1st',null,null,'4th');
//--></script>
|
Which produces the following:
Output:
First = 1st
Second = 2nd
Third = 3rd
Output:
First = 1st
Fourth = 4th
Articles | Arguments Array |
back
|