Categories:
Partners

Creating Robust Functions

Before we continue, its best to first describe what we consider to be a robust function. A robust function, in our view, is one that can accept any number of parameters, as opposed to a "normal" function, where the number of parameters accepted is preset, and cannot be altered. Lets get started, shall we?

Quick overview of a "normal" function

There are, in general, two kinds of functions in JavaScript- ones with no parameters, and ones with. The below shows an example of each:

//function with no parameters
function sayhello(){
alert("hello")
}

//function with one parameters
function saywhat(word){
alert(word)
}

The first function simply blindly yells out "hello" when we call it; the second one's a little more sophisticated, and says whatever we put into its mouth...uh, parameter. A function by no means have to have simply one parameter. It could have 2, or 3, or even a hundred. Having said that, an obvious limitation to normal functions arise. A function could have 1, 2, 3, or even a hundred parameters, but it can't have it all. It has to pick one- "Do I want 2 parameters, or a hundred? Do I want butter with my toast, or jam?" Once a function is set to accept 2 parameters, shooving in a hundred will cause it to explode. Definitely not what you want, unless you're goal is to make a JavaScript bomb. Here comes the arguments array to the rescue!