jslint

How to split a long regular expression into multiple lines in JavaScript?

巧了我就是萌 提交于 2019-11-26 04:18:04
问题 I have a very long regular expression, which I wish to split into multiple lines in my JavaScript code to keep each line length 80 characters according to JSLint rules. It\'s just better for reading, I think. Here\'s pattern sample: var pattern = /^(([^<>()[\\]\\\\.,;:\\s@\\\"]+(\\.[^<>()[\\]\\\\.,;:\\s@\\\"]+)*)|(\\\".+\\\"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$/; 回答1: You could convert it to a string and create the expression by

JavaScript function order: why does it matter?

我们两清 提交于 2019-11-26 03:47:11
问题 Original Question: JSHint complains when my JavaScript calls a function that is defined further down the page than the call to it. However, my page is for a game, and no functions are called until the whole thing has downloaded. So why does the order functions appear in my code matter? EDIT: I think I may have found the answer. http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting I am groaning inside. Looks like I need to spend ANOTHER day re-ordering six thousand lines of

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

拈花ヽ惹草 提交于 2019-11-26 03:40:59
问题 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? 回答1: 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 .... 回答2: 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() { };

Immediate function invocation syntax

徘徊边缘 提交于 2019-11-26 01:44:57
问题 There is a JSLint option, one of The Good Parts in fact, that \"[requires] parens around immediate invocations,\" meaning that the construction (function () { // ... })(); would instead need to be written as (function () { // ... }()); My question is this -- can anyone explain why this second form might be considered better? Is it more resilient? Less error-prone? What advantage does it have over the first form? Since asking this question, I have come to understand the importance of having a

What is the difference between `new Object()` and object literal notation?

荒凉一梦 提交于 2019-11-26 01:24:23
问题 What is the difference between this constructor-based syntax for creating an object: person = new Object() ...and this literal syntax: person = { property1 : \"Hello\" }; It appears that both do the same thing, although JSLint prefers you use object literal notation. Which one is better and why? 回答1: They both do the same thing (unless someone's done something unusual), other than that your second one creates an object and adds a property to it. But literal notation takes less space in the

How to initialize an array&#39;s length in javascript?

痴心易碎 提交于 2019-11-26 00:49:52
问题 Most of the tutorials that I\'ve read on arrays in JavaScript (including w3schools and devguru) suggest that you can initialize an array with a certain length by passing an integer to the Array constructor using the var test = new Array(4); syntax. After using this syntax liberally in my js files, I ran one of the files through jsLint, and it freaked out: Error: Problem at line 1 character 22: Expected \')\' and instead saw \'4\'. var test = new Array(4); Problem at line 1 character 23:

How to fix jslint error &#39;Don&#39;t make functions within a loop.&#39;?

百般思念 提交于 2019-11-26 00:48:09
问题 I am working on making all of our JS code pass through jslint, sometimes with a lot of tweaking with the options to get legacy code pass for now on with the intention to fix it properly later. There is one thing that jslint complains about that I do not have a workround for. That is when using constructs like this, we get the error \'Don\'t make functions within a loop.\' for (prop in newObject) { // Check if we\'re overwriting an existing function if (typeof newObject[prop] === \"function\"

Why avoid increment (“++”) and decrement (“--”) operators in JavaScript?

人盡茶涼 提交于 2019-11-25 22:45:39
问题 One of the tips for jslint tool is: ++ and -- The ++ (increment) and -- (decrement) operators have been known to contribute to bad code by encouraging excessive trickiness. They are second only to faulty architecture in enabling to viruses and other security menaces. There is a plusplus option that prohibits the use of these operators. I know that PHP constructs like $foo[$bar++] has may easily result with off-by-one errors, but I couldn\'t figure out a better way to control the loop than a

What does “use strict” do in JavaScript, and what is the reasoning behind it?

风格不统一 提交于 2019-11-25 21:42:49
问题 Recently, I ran some of my JavaScript code through Crockford\'s JSLint, and it gave the following error: Problem at line 1 character 1: Missing \"use strict\" statement. Doing some searching, I realized that some people add \"use strict\"; into their JavaScript code. Once I added the statement, the error stopped appearing. Unfortunately, Google did not reveal much of the history behind this string statement. Certainly it must have something to do with how the JavaScript is interpreted by the

Immediate function invocation syntax

我与影子孤独终老i 提交于 2019-11-25 18:51:11
There is a JSLint option, one of The Good Parts in fact, that "[requires] parens around immediate invocations," meaning that the construction (function () { // ... })(); would instead need to be written as (function () { // ... }()); My question is this -- can anyone explain why this second form might be considered better? Is it more resilient? Less error-prone? What advantage does it have over the first form? Since asking this question, I have come to understand the importance of having a clear visual distinction between function values and the values of functions. Consider the case where the