|
Partners
|
|
<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 a function.
With the help of the arguments array, in addition to a minor change when defining a function, we can create a function that accepts any number of parameters.
The first order of buisness is to define a robust function. A function as such looks exactly the same as a function with say, two or three parameters, only that the parameters are not explicitly specified.
function robust(){
}
Now that that's out of the way, the remaining work is to create a mechanism using the arguments array that dynamically retrieves all of the parameters that get passed in, so the remaining portion of the function can do its job with them. A simple for loop will do nicely:
function robust(){
for (i=0;i<robust.arguments.length;i++)
//do whatever
}