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

You are here:  Home > Developers > Sample Code > JavaScript > JavaScript Sample Code
JavaScript Sample Code
 iPlanet Developers


Developer Program
  Membership
  One-to-One Support
  Newsgroups
  Member Services

Developer Publications
  View Source
  Developer News

Documentation
  Technical Manuals
  White Papers
  TechNotes
  Sample Code
  FAQs
  Books

Technologies
  Application Server   CSS
  DOM
  CORBA
  Directory & LDAP
  Dynamic HTML
  Java
  JavaScript
  Linux
  RDF
  Security
  SSJS
  XML
  XUL

Developer Downloads
  Tools & SDKs
  Patches

iPlanet Products
  Technical Resources










spacer
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>


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

spacer spacer


                                                       
iPlanet International | Year 2000 | Site Map | Feedback
Products | Solutions | Support | Services | Download | About Us | Developer
© 2000 Sun-Netscape Alliance. All Rights Reserved  Privacy Policy