Robust JavaScript: Check arguments and throw exception if unset
utility function to check if arguments passed to a function were set:
usage:
relevant reading:
blog about JavaScript guard clauses:
https://andrew.stwrt.ca/posts/js-guards/
book - JavaScript Allongé:
https://leanpub.com/javascriptallongesix/read
utility function to check if arguments passed to a function were set:
Utilities = Utilities || {};
/**
* @function
*/
Utilities.checkArgsSet = function () {
if(arguments.length === 0) {
throw "no arguments were passed to Utilities.checkArgsSet";
}
for(var i = 0, length = arguments.length; i < length; i++) {
var arg = arguments[i];
if(typeof arg === "undefined") {
throw "bad arguments - argument not set!";
}
}
};
usage:
/**
* @function
*/
Utilities.doSomething = function (one, two) {
Utilities.checkArgsSet(one, two);
//continue safely ...
};
relevant reading:
blog about JavaScript guard clauses:
https://andrew.stwrt.ca/posts/js-guards/
book - JavaScript Allongé:
https://leanpub.com/javascriptallongesix/read
Comments
Post a Comment