assistance require resolving jslint errors
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
};
}
}
A.) See: http://api.jquery.com/jQuery.each/
you can use:
$.each(collection, function() {
doSomething(this); // this refers to the current iteration
});
B.) If you aren't actually using "undefined" you aren't protecting it from anything
C.) I'm not going to bother with regex lol EDIT: Perhaps it wants [A-Z0-9\-]
D.) You are concatenating string with number. Try 'string' + Date.getTime().toString()
instead
See also JSLint Type Confusion: function and object with jQuery .css() for type confusion stuff, there are some oddities that I don't agree with
E.) Again I'm not going to try for the regex EDIT: Here's an identical question though: JSLint "insecure ^" in regular expression
F.) If you can create your function once outside of the loop and then use it inside the loop (as long as you do it well) it is a significant performance increase.
I see others have answered, so I'll at least put an attempt in for c)
c. JS Lint: Unescaped '-'.
if (value.match(/^[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$/i)) { return true; }
C. Add a backslash before the - in 9-
and +-
Sorry, can't help you with E), that regex looks ok to me.
精彩评论