The idea is very simple: ensure that a function is called with the expected number of parameters.
A function defined like:
could be called with:
- zero parameters: name and surname will be undefined.
- one parameter: the parameter will be assigned to name, surname will be undefined.
- two parameters: the former parameter will be assigned to name, the latter to surname.
- three or more parameters: the first two parameters will be assigned to name and surname respectively, the next ones could be accessed via
You might want to ensure the function is always called with two parameters, so scenarios like this one won’t happen:
Let’s do the magic by defining a method in the
Simply adding to our previous function the following:
Protip: adding a function to
Function prototype is usually NOT a good idea.