Home / Advanced JavaScript Tutorial / Creating Robusts Functions

 

 

CodingForums
Having trouble with scripting? Visit our help forum to get the answers you need.

Jump to...
-Free JavaScripts
-JS tutorials
-JS Reference
-DHTML/CSS

-Free applets
-Web Tutorials
-Link to Us!

- CSS Drive
- JavaScript Menus
- PHP Resources
- Hotels & Vacations
- Java Online Casinos
- Web hosting Search
 

Red_CurlyC035.gif (285 bytes) The arguments array- the secret to robustness

The arguments array is a secretive little object that exists inside every function. As you effortlessly pass in parameters into a function, the arguments array is working hard not only storing the values of these parameters into one of its array element, but jotting down the total number of parameters passed as well. This is realized through the following syntax:

functionname.arguments.length="contains the total number of parameters"
functionname.arguments[0]="contains the value of the first parameter in the function"
functionname.arguments[1]="contains the value of the second parameter in the function"
"
"

Note: The functionname proceeding the arguments array is optional.

You're baffled, and I don't blame you. The best way to understand this object is to see it at work, so lets do just that. The below's a function that accepts three parameters. Lets use the arguments array to expose that, and more!

function mynumbers(firstword, secondword, thirdword){
alert("Total # of parameters="+mynumbers.arguments.length)
for (i=0;i<arguments.length;i++)
alert(mynumbers.arguments[i])
}

The above's a basic function that accepts three parameters. Lets call it now, and see what the arguments array has to offer:

<script>
//call the function
mynumbers("How","are","you")
</script>

What's the big deal with this example, you say. Plenty! Sure, its obvious from simply looking at the function what the total number of parameters is, and the values of each parameter, but we never could tell by using JavaScript codes. Now, we could, through the arguments array of each function.

-Tutorial introduction
-Quick overview of a "normal" function
-The limitations of a normal function
-The arguments array- the secret to robustness
-Creating robust functions
-Examples

Creating robust functions Small4.gif (1046 bytes)


http://javascriptkit.com
Copyright © 1997-2005 JavaScript Kit. NO PART may be reproduced without author's permission.