jslint

JSlint error 'Don't make functions within a loop.' leads to question about Javascript itself

走远了吗. 提交于 2019-11-26 18:53:49
I have some code that invokes anonymous functions within a loop, something like this pseudo example: for (i = 0; i < numCards; i = i + 1) { card = $('<div>').bind('isPopulated', function (ev) { var card = $(ev.currentTarget); .... JSLint reports the error 'Don't make functions within a loop.' I like to keep my code JSLint clean. I know I can move the anonymous function out of the loop and invoke it as a named function. That aside, here's my question: Would a Javascript interpreter really create an instance of the function per iteration? Or is there really only one function instance "compiled"

What is array literal notation in javascript and when should you use it?

谁说胖子不能爱 提交于 2019-11-26 18:43:59
JSLint is giving me this error: Problem at line 11 character 33: Use the array literal notation []. var myArray = new Array(); What is array literal notation and why does it want me to use it instead? It shows here that new Array(); should work fine... is there something I'm missing? CookieOfFortune array literal notation is where you define a new array using just empty brackets. In your example: var myArray = []; It is the "new" way of defining arrays, and I suppose it is shorter/cleaner. The examples below explain the difference between them: var a = [], // these are the same b = new Array()

What does the JSLint error 'body of a for in should be wrapped in an if statement' mean?

人盡茶涼 提交于 2019-11-26 18:42:42
问题 I used JSLint on a JavaScript file of mine. It threw the error: for( ind in evtListeners ) { Problem at line 41 character 9: The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype. What does this mean? 回答1: First of all, never use a for in loop to enumerate over an array. Never. Use good old for(var i = 0; i<arr.length; i++) . The reason behind this is the following: each object in JavaScript has a special field called prototype . Everything

Should I use JSLint or JSHint JavaScript validation? [closed]

安稳与你 提交于 2019-11-26 18:00:27
I am currently validating my JavaScript against JSLint and making progress on, it's assisting me to write better JavaScript - in particular in working with the Jquery library. I have now come across JSHint , a fork of JSLint . So I am wondering for web applications, which are very much JavaScript was driven, which is the better or most applicable validation tool to work against: JSLint or JSHint? I want to decide now on a validation mechanism and moving forward, use this for client side validation. And Difference between jshint and jslint? Please explain in single javascript example. Links:

JSLint Error: Unexpected 'this'

会有一股神秘感。 提交于 2019-11-26 16:04:27
问题 Having trouble understanding why JSLint is surprised by my use of this in the following code: function testConstr (x) { 'use strict'; this.joker = "Whyyy sooo seriousss?"; this.x = x; } For both property assignments, JSLint says: Unexpected 'this'. How do I correct my code? 回答1: Your code might be perfectly correct (it might also be problematic, depending on how you call testConstr ). My suggestion is: tell JSLint to shut up Or don't use JSLint at all. 回答2: So in other words, JSLint doesn't

What&#39;s wrong with var x = new Array();

隐身守侯 提交于 2019-11-26 13:22:48
In JSLint, it warns that var x = new Array(); (That's not a real variable name) should be var result = []; What is wrong with the 1st syntax? What's the reasoning behind the suggestion? Shog9 Crockford doesn't like new . Therefore, JSLint expects you to avoid it when possible. And creating a new array object is possible without using new .... It's safer to use [] than it is to use new Array() , because you can actually override the value of Array in JavaScript: Array = function() { }; var x = new Array(); // x is now an Object instead of an Array. In other words, [] is unambiguous. Parris It

JSLint: control comments (selective ignore)

≯℡__Kan透↙ 提交于 2019-11-26 13:09:04
问题 Does JSLint have anything like JavaScript Lint\'s control comments (e.g. /*jsl:fallthru*/ ) to make it ignore certain passages? 回答1: Put /*ignore jslint start*/ before and /*ignore jslint end*/ after the code to be ignored. Ex: function ignore(){ /*ignore jslint start*/ var x; var y; /*ignore jslint end*/ } Or export JsLint settings, define your IgnoreErrorStart/ IgnoreErrorEnd symbols and import. Edit Some folks may confuse this answer with JSHint. In that case, use these: /*jshint ignore

JSLint: was used before it was defined

我怕爱的太早我们不能终老 提交于 2019-11-26 12:57:30
问题 Hi I have the 3 javascript files. jquery.js utility.js file1.js In file1.js I have jQuery.noConflict() jQuery(document).ready(function($) { // .... }); I get an error \'jQuery\' was used before it was defined. and \'document\' was used before it was defined. How do I safely get rid of this warning. If I do var document = document || {}; then in my utility.js if it is used, it would be null in IE and ok in firefox. What is the best solution to this? 回答1: From the documentation JSLint also

JSLint reports “Insecure ^” for my regex — what does that mean?

大城市里の小女人 提交于 2019-11-26 12:31:03
问题 I\'m trying to get my Javascript code 100% JSLint clean. I\'ve got a regular expression: linkRgx = /https?:\\/\\/[^\\s;|\\\\*\'\"!,()<>]+/g; JSLint reports: Insecure \'^\' What makes the use of the negation of the character set \"insecure\" ? 回答1: [^\s;|\\*'"!,()<>] matches any ASCII character other than the ones listed, and any non-ASCII character. Since JavaScript strings are Unicode-aware, that means every character known to Unicode. I can see a lot of potential for mischief there. Rather

JSLint Expected &#39;===&#39; and instead saw &#39;==&#39;

北城余情 提交于 2019-11-26 11:58:57
问题 Recently I was running some of my code through JSLint when I came up with this error. The thing I think is funny about this error though is that it automatically assumes that all == should be ===. Does that really make any sense? I could see a lot of instances that you would not want to compare type, and I am worried that this could actually cause problems. The word \"Expected\" would imply that this should be done EVERY time.....That is what does not make sense to me. 回答1: IMO, blindly using