1

jslint requires that function definitions are made before the calling of the function.

However, I would have thought that the dynamic compiler is smart enough to look ahead for the definition.

Is is just best practice to define your function before you call it?

In my case the logic would be more clear if I called the function before I defined it.

I don't see and option at jslint to tolerate function calls before function definitions.

  • the answer is just another link, I'm asking about best practice. –  Jun 09 '14 at 15:30
  • 3
    If you don't like jslint's opinions, then don't use it. Maybe try a more configurable linter like http://jshint.com . The "best practice" is a matter of opinion. – cookie monster Jun 09 '14 at 15:31
  • If you use function expressions, you must define them before calling. If you use function declarations, it doesn't matter. – Oriol Jun 09 '14 at 15:31
  • In my opionion function declarations can be written after they are called as long as they are in scope, hoisting will take care of the rest in all browsers known to man. The linter isn't always right. – adeneo Jun 09 '14 at 15:31
  • @user3293653 See https://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/ – Oriol Jun 09 '14 at 15:35
  • No, `function foo() {}` is declaring a function, it's a *function declaration*, while setting variables is always an expression, so `var foo = function() {}` is a *function expression* – adeneo Jun 09 '14 at 15:36
  • As a sidenote, a **definition** for javascript funtions is something you find in the [Ecma specs](http://www.ecma-international.org/ecma-262/5.1/#sec-13) – adeneo Jun 09 '14 at 15:38

1 Answers1

0

As noted in one of the comments, "Best Practice" here is a matter of opinion. JSLint complains about a lot of things that most people generally ignore, such as

if(something)
    console.log("Doing this without curly brackets!");

or

console.log("I'm not going to use a semicolon")

or maybe

calling('Sheryl');

function calling(who) { 
    console.log(who);
}

Sure, there may be a fringe case where a poor implementation of the standards in some minority browser doesn't like the shorthand if statement there, or perhaps an oddball engine will get all kinds of confused by the lack of semicolon. "Best Practices" may dictate that you can do a shorthand if but you should always end a statement with a semicolon. Ask your coworkers or, if you work on your own, set a standard and stick to it. The best practices are the ones you adhere to.

phatskat
  • 1,797
  • 1
  • 15
  • 32