76

I'm using JSLint to verify most of my external Javascript files, but the largest amount of errors I'm getting is from functions being used before they're defined.

Is this really an issue I should worry about?

It seems Firefox, IE7 and Chrome don't care. Functions like the popular init() (which I use often) normally stick at the top as that makes sense to me (I like to pretend it's analogous to main()) will, according to JSLint, need to be pushed to the bottom of the file.

8 Answers 8

87

As this is the top rated google hit and other people might not be seeing it at first in the jslint tool, there is a option called "Tolerate misordered definitions" that allows you to hide this type of error.

/*jslint latedef:false*/
4
  • 5
    Setting that option to true does not seem to "solve" this problem for me. Dec 13, 2012 at 19:19
  • I am experiencing the same issue as Markus.
    – M. Herold
    May 23, 2013 at 18:55
  • Could you share your javascript? @M.Herold
    – kontur
    May 24, 2013 at 12:59
  • 4
    @PeterMajeed No, it's not. Chris is not asking about how to tolerate misordered definitions. Jan 14, 2014 at 10:25
69

If you declare functions using the function keyword, you can use them before they're declared. However, if you declare a function via another method (such as using a function expression or the Function constructor), you have to declare the function before you use it. See this page on the Mozilla Developer Network for more information.

Assuming you declare all your functions with the function keyword, I think it becomes a programming-style question. Personally, I prefer to structure my functions in a way that seems logical and makes the code as readable as possible. For example, like you, I'd put an init function at the top, because it's where everything starts from.

1
  • 2
    I prefer init-method atop as well. So functions it uses go below. How to ignore or hack this error as it blocks me from seeing next JsLint errors in code? I want a general approach to keep my style, but see this error not.
    – Zon
    Jan 11, 2016 at 8:38
33

If you're using jshint you can set latedef to nofunc, which will ignore late function definitions only.

Documentation - http://www.jshint.com/docs/options/#latedef

Example usage:

/* jshint latedef:nofunc */

noop();

function noop() {}

Hope this helps.

2
  • This is the 2014 solution.
    – Deathspike
    Jul 4, 2014 at 18:46
  • 2
    Is this solution for jshint only? I use "Brackets" and I get a lot of lint warnings. Should lint be compatible with this hint solution. I didn't get it to work. Oct 16, 2014 at 14:50
11

From jslint's website (http://www.jslint.com/lint.html), you can read about a /*global*/ directive that allows you to set variables that are assumed to be declared elsewhere.

Here is an example (put this at the top of the file):

/*global var1,var2,var3,var4,var5*/

The :true :false is not actually needed from my experience, but it looks like it's recommended from what I read on the site.

Make sure the initial global statement is on the same line as /*, or else it breaks.

1
  • 3
    Also for me the word global has to be directly after the asterisk, with no spaces or else it will be ignored. Sep 2, 2013 at 21:49
2

To disable this warning in jshint for all files, place this in your .jshintrc file:

{
   "latedef": false
}
2

In your .jshintrc file, set:

  "latedef": "nofunc",
2

it is very unfortunate the latedef option was removed. This is essential when trying to create a 'class' with an interface at the top, ie,

function SomeClass() {
   var self = this;
   self.func = func;

   function func {
      ...
   }
}

This style is very common but does not pass jsLint because func is 'used' before being defined. Having to use global for each 'member' function is a total pain.

0

You can always declare the offending function at the top

eg: var init;

.... but then you'll have to remove the "var" when you get to the true definition further down:

init = function() { };

1
  • 2
    Warning: init = function(){} is not the same as function init() {} ECMAScript has different rules for anonymous functions, that is what the first is. Oct 17, 2013 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.