In JavaScript all functions and variables are often ‘hoisted’ to global scope, effectively making them global variables. This is potentially bad, as the following demonstrates:

if (true) {
    var x = 0;
}

alert(x); //alerts "0"

Here, even though ‘x’ is defined in a block, it is given global scope, and hence accessible outside the block. (For a demo, see http://jsfiddle.net/vRmLL/)

If we define our variable within a function, however, the scope is retained within the function. So the following will not work:

function SomeTest() {
    var y = 0;
}

alert(y); //undefined – a scoped variable - just what we want!

So, by using functions we can introduce scoping into JavaScript:

(function() {
    var z = 0;
})();

alert(z); //undefined – just what we want!

The last () executes everything in the body of the anonymous function. This pattern is called a closure.

This is a very simplistic overview of them. A more comprehensive one can be found in Morris Johns’ article JavaScript Closures for Dummies.

, , ,

With this message we take a look at JavaScript closures. Without wanting to engage into a discussion about what they or their relative merits are, we can examine how this message might arise. Take the following code:

var a = 0;
(function(a) {
    alert(a);
}(a));

The parentheses around the function are necessary, because when omitted, the compiler assumes it is a Function Statement (see links below).When omitted, our code looks like this:

var a = 0;
function(a) {
    alert(a);
}(a);

This code is incorrect and will fail to execute.

In addition to this message you will also receive the message “Missing name in function statement.”

Some Links

For more information on closures, refer to Closures in JavaScript by James Padolsey.

The above function code follows a convention more commonly known as the JavaScript Module Pattern

To demystify the mire of Function Declarations, Function Expressions and Function Statements, refer the article Named function expressions demystified by Juriy “kangax” Zaytsev.

Also thanks to StackOverflow for helping me answer this: Question – Solution for JSLint Errors

Be careful where you place your parentheses. You may get another JSLint message Move the invocation into the parens that contain the function.

A Guide To JSLint Messages

This article is one of a series on the error and warning messages produced by JSLint.

, , , ,

With this message we move deep into the heart of JavaScript closures. Without wanting to engage into a discussion about what they or their relative merits are, we can examine how this message might arise. Take the following code:

var a = 0;
(function(a) {
    alert(a);
})(a);

The parentheses around the function are necessary, because when omitted, the compiler assumes it is a Function Statement (see links below).

When referring to ‘Invocation’ we actually mean the bit of code (a) at the very end of the block of code. Although the code itself will execute perfectly, JSLint prefers that everything is more self-contained, so insists on the following convention instead:

var a = 0;
(function(a) {
    alert(a);
}(a));

This is almost identical, except the invocation (a) now appears within the parentheses that wrap the function.

The most convincing for me is that the initial parentheses isolates the block of code that is being invoked by the second set of parentheses. In addition to this, JSLint has been designed specifically to eliminate conventions and behaviours that have been proven to lead to bugs and issues, so it is possible that sometime in the past the first convention has yielded a bug or some sort of issue.

Some Links

For more information on closures, refer to Closures in JavaScript by James Padolsey.

The above function code follows a convention more commonly known as the JavaScript Module Pattern

To demystify the mire of Function Declarations, Function Expressions and Function Statements, refer the article Named function expressions demystified by Juriy “kangax” Zaytsev.

Also thanks to StackOverflow for helping me answer this: Question –  Solution for JSLint Errors

A Guide To JSLint Messages

This article is one of a series on the error and warning messages produced by JSLint.

, , , , , ,