A little known functionality of JavaScript is its ability to
accept one or more arguments within a single function. This
advantage saves coding time as well as memory consumption. Thus,
instead of writing different functions for different arguments,
you can write a single function which handles many arguments or
combinations of arguments.
An instance of where you may want to use this is in programming
forms. If a form changes over time, and if you have used this
functionality to validate your form, you wouldn't have to rewrite
the code, but simply modify the existing code.
Here's an example:
<HTML>
<HEAD>
<SCRIPT Language="JavaScript">
function foo() {
// ARRAY OF ARGUMENT VALUES
var argv = foo.arguments;
// THE NUMBER OF ARGUMENTS PASSED TO THIS FUNCTION
var argc = argv.length;
// FOR FORMATTING PURPOSES ONLY
document.write("<BR><BR>\n\n");
// LOOP THROUGH THE ARGUMENTS (0 THROUGH N-1)
for (var i = 0; i < argc; i++) {
// WRITE OUT THE ARGUMENT VALUES
document.write("<BR>\nValue of Argument " + i + ": " +
argv[i]);
}
}
</SCRIPT>
</HEAD>
<BODY>
<CENTER><H1>Arguments passed to function
"foo"</H1></CENTER>
<SCRIPT>
// CALLING THE FUNCTION THREE TIMES:
foo(1, "two", 3);
foo("Testing ONE Argument.");
foo(3.14159, "Netscape", "Communicator", "Coming", "Soon!");
</SCRIPT>
</BODY>
</HTML>
- Related Reading
Any sample code included above is provided for your use on an "AS IS" basis, under the Netscape License Agreement - Terms of Use