I have a function that is like this –
var myFunction = function(arg1, arg2, arg3, arg4, arg5, arg6, arg7){
if(typeof arg1 != 'undefined' && typeof arg2 attributeId != 'undefined' && typeof arg3 != 'undefined'){ //and so on for all the arguments
//do something with all of the arguments
}
}
Basically, I’m checking if all 7 of the arguments exist before doing something.
This looks ugly and is unreadable. Can you suggest a more elegant way to achieve the same thing?
It’s possible to use the
argumentsvariable to determine how many arguments were passedHowever this still won’t tell you that all of them are defined values. In order to determine if they are all defined you must individually check them as it’s legal to pass undefined in the middle of an argument list
Probably the best approach is to abstract this out to a separate function