★ wanayoo — archive 1999 http://developer.netscape.com/docs/examples/javascript/pass_arg.htmlNouvelle recherche | Portail wanayoo


Click Here!

PASSING A VARIABLE NUMBER OF ARGUMENTS TO A JAVASCRIPT FUNCTION

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>


ADDITIONAL READING
JavaScript Known Bug List
Sample Code Area
Books
Articles
JavaScript Course
Dynamic Style Sheets
Other JavaScript Resources
JavaScript Language Spec
JavaScript Guide
What's New in JavaScript for Navigator 4.0
JavaScript Scripting Tools

VIEW SOURCE ARTICLES

Beyond Data Basics: Writing JavaScript Database Applications, Part 1
Beyond Data Basics: Writing JavaScript Database Applications, Part 2
JavaScript Date Object Techniques
Scripting Layer Effects and Transitions
Detecting a JavaScript Client
Bringing Images to Life with JavaScript

MEMBERS RESOURCES

JavaScript Newsgroup
JavaScript FAQ

For more Internet development resources, try Netscape TechSearch.


DevEdge Online FAQ
Developer Response Center
Join DevEdge Program
Copyright © 1999 Netscape Communications Corporation.
This site powered by: Netscape Enterprise Server and Netscape Compass Server.