JSLint Messages – ‘{a}’ is a function.

JavaScript functions can be written as a declaration or as expression, as in the following examples:

function a(){} //function declaration
var a = function(){};  //function expression

The first line declares a function, and the second declares a variable. For the purposes of this discussion, we can think of the key word ‘function’ as having a similar purpose to the keyword ‘var’, namely, that it declares something (‘a’ in both cases).

Both are perfectly legitimate ways of declaring a function, as is the following:

var a = function(){alert("1";)};
a = function(){alert("2");};

All we are doing here is reassigning the variable ‘a’ with a different function expression. JSLint will not complain about this in the slightest, simply because there is nothing wrong with this. However, what if we substitute the first line above with a function declaration, JSLint starts complaining with ‘a’ is a function:

function a(){alert("1";)}; //JSLint error: 'a' is a function
a = function(){alert("2");};

Instead of ‘a’ being a ‘var’, it is a ‘function’, but in the second statement we attempt to use it as a ‘var’. In other words we are attempting to turn it from a function declaration to a  function expression.

You may find out that the interpreter is tolerant of this, but it is still indicative of something potentially problematic, and possibly not what was intended.

For more information on function declarations and function expressions, refer to this article at JavaScript, JavaScript. Related, and also of interest is this article on JavaScript Scoping and Hoisting.

A Guide To JSLint Messages

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

One thought on “JSLint Messages – ‘{a}’ is a function.

  1. Pingback: JSLint – A Guide To JSLint Messages | James Wiseman

Leave a Reply