Javascript warning: "Function X is used before it is declared"

Functions can be defined in two ways:

function foo() {}


var foo = function() {};

The warning "Function X is used before it is declared" is only valid for the second format.  

The first version should not cause this warning, since it is "hoisted" within the scope it is in.

Comment actions Permalink

This is quite typical for a linter to catch. Despite the hoisting, it's still considered by many to be bad practice. I've never personally had an issue with it though, and like to leverage the hoisting of functions to organize my code (i.e. I put all my function definitions at the bottom of the file).

However, if you want to disable this inspection, there are at least two different ways:

  1. The easiest way is, with the cursor in the function's name hit Alt+Enter (or click the light bulb in the margin) then goto "Inspection 'Function is used before it is declared'" > "Configure inspection severity", then select "Do not show" in the displayed dialog.
  2. Or to see all of the available inspections, open ReSharper's Options, goto Code Inspection > Inspection Severity > JS > Code Style; Find and set the option "Function is used before it is declared" to "Do not show".

Notice that this setting only applies to functions. Variables being used before they're defined will still show the warning (as they should).

It seems to me that setting is the same as jshint/jslint's latedef: "nofunc" option. ReSharper just made it less conventional to configure. It would be nice if they just used an existing .jshintrc file instead.

Hope that helps!
Comment actions Permalink

Agree with Glen.

This message should not be shown for the functions declared as `function X(){...}`.

I think this should be posted as a bug of R#.


Please sign in to leave a comment.