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

Partially it depends on whether you’re using a function expression or a function declaration. They’re different things, they happen at different times, and they have a different effect on the surrounding scope. So let’s start with the distinction. A function expression is a function production where you’re using the result as a right-hand value — e.g., … Read more

JSLint is suddenly reporting: Use the function form of “use strict”

Include ‘use strict’; as the first statement in a wrapping function, so it only affects that function. This prevents problems when concatenating scripts that aren’t strict. See Douglas Crockford’s latest blog post Strict Mode Is Coming To Town. Example from that post: (function () { ‘use strict’; // this function is strict… }()); (function () … Read more

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

Extending @KooiInc answer, you can avoid manually escaping every special character by using the source property of the RegExp object. Example: var urlRegex= new RegExp(” + /(?:(?:(https?|ftp):)?\/\/)/.source // protocol + /(?:([^:\n\r]+):([^@\n\r]+)@)?/.source // user:pass + /(?:(?:www\.)?([^\/\n\r]+))/.source // domain + /(\/[^?\n\r]+)?/.source // request + /(\?[^#\n\r]*)?/.source // query + /(#?[^\n\r]*)?/.source // anchor ); or if you want to … Read more

JavaScript function order: why does it matter?

tl;dr If you’re not calling anything until everything loads, you should be fine. Edit: For an overview which also covers some ES6 declarations (let, const): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Scope_Cheatsheet This weird behavior depends on How you define the functions and When you call them. Here’s some examples. bar(); //This won’t throw an error function bar() {} foo(); //This … Read more