I am currently running JSLint against the javascript in my web application and getting some errors that I require assistance with resolving.
a. First error I am getting is: JS Lint: Unused Variable 'n'.
$.each(collection, function (n, item) {
var temp = item.Id;
// do further processing
});
b. I have all my javascript declared in a self executing function like such:
(function ($, undefined) {
// further javascript code
}
(jQuery));
The above pattern can protect the $ from conflicting with other JavaScript libraries and also protect undefined from being redefined. However I get these errors from it:
JS Lint: Expected an identifier and instead saw 'undefined' (a reserved word). JS Lint: Unused Variable 'undefined'.
c. JS Lint: Unescaped '-'.
if (value.match(/^[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$/i)) {
return true;
}
d. JS Lint: Type confusion: 'printer-': string and '(': number.
var frameName = "printer-" + new Date().getTime();
I get numerous errors of Type confusion, sometimes with numbers, integers and other data types. Any idea as to how I can prevent getting these?
e. JS Lint: Insecure '^'.
var value = value.replace(/[^\d\.,\-]/gi, '');
f. JS Lint: Don't make functions within a loop.
for (i = 0, l = txts.length; i < l; i += 1) {
if (/^[0-9]+$/.test(txts[i].getAttribute("maxlength"))) {
var func = function () {
//do some processing
};
}
}