All,
I use JSLint to validate my JS files. In my most recent project, I am using the following format to set default values for a number of JavaScript functions (further detailed here):
function(a, b, option) {
option = arguments.length > 2 ? option : "some default value";
// ...
}
This however causes the latest build of JSLint to produce the following error:
"Do not mutate parameter 'option' when using 'arguments'."
I am aware that using the more common method for assigning defaults (i.e., option = option || {};) supresses the error; however, this will produce incorrect behaviour if I intend to pass a falsey value to option.
Is the only solution to this issue to introduce a new variable? e.g.:
var option2 = arguments.length > 2 ? option : "some default value";
I guess JSLint warns you since you try to modify one of the input arguments by using a check with the arguments keyword. JSHint, however, does give me any warning when trying your code.
A solution to your problem would be to check if
optionis defined or not, that way you go around the problem with sending in falsy values:If you find that it is cumbersome to write this
typeofcheck everytime, create anisDeffunction:// Simon A.