javascript - JSLint "out of scope" error due to function ordering? - Stack Overflow

admin2025-04-18  0

JSLint seems to be picky about function ordering.

This passes fine:

function a() {
    'use strict';
    return 1;
}

function b() {
    'use strict';
    a();
}

While this gives an 'a' is out of scope error message:

function b() {
    'use strict';
    a();
}

function a() {
    'use strict';
    return 1;
}

Is this by design? Should I care? How can it be avoided in larger (more plex) cases, where it might not always be possible to give the functions a clear order?

JSLint seems to be picky about function ordering.

This passes fine:

function a() {
    'use strict';
    return 1;
}

function b() {
    'use strict';
    a();
}

While this gives an 'a' is out of scope error message:

function b() {
    'use strict';
    a();
}

function a() {
    'use strict';
    return 1;
}

Is this by design? Should I care? How can it be avoided in larger (more plex) cases, where it might not always be possible to give the functions a clear order?

Share Improve this question asked Jan 5, 2016 at 14:49 CodemonkeyCodemonkey 4,8297 gold badges53 silver badges87 bronze badges 5
  • The error should be something like, Functions should be defined before using them – Tushar Commented Jan 5, 2016 at 14:50
  • 2 "JSLint seems to be picky about function ordering." — Yes, it is. For that matter… "JSLint seems to be picky" is true as well. – Quentin Commented Jan 5, 2016 at 14:51
  • Some linters want you to define functions before they are referenced, JavaScript doesn't care though. – Ruan Mendes Commented Jan 5, 2016 at 14:53
  • 2 stackoverflow./questions/7609276/… – epascarello Commented Jan 5, 2016 at 14:54
  • The ment from @epascarello is absolutely essential here. This ordering warning really is an error worth addressing (see answer, below). Also, do you have an example of "where it might not always be possible to give the functions a clear order"? Usually, as I remark below, that's a code smell worth checking out. – ruffin Commented Jan 5, 2016 at 15:34
Add a ment  | 

2 Answers 2

Reset to default 4

JSLint/JSHint expect you to define functions before you reference them. However, JavaScript doesn't care because functions and variables are hoisted.

You can change your code style, or tell the linter to ignore it using http://jshint./docs/options/#latedef

/* jshint latedef:nofunc */
function b() {
    'use strict';
    a();
}

function a() {
    'use strict';
    return 1;
}

See https://stackoverflow./a/23916719/227299

@epascarello's link to where this was discussed for JSHint really is absolutely essentially here, because this isn't just a question of style.

Let's hit the high points of the excellent answer at that question, as it applies to JSLint as well.*

There are two ways to define functions: Function declaration and function expression. The difference is annoying and minute, so let's just say this slightly wrong thing: If you're writing it like function name() {}, it's a declaration, and when you write it like var name = function() {} (or an anonymous function assigned to a return, things like that), it's a function expression.

bar(); //This won't throw an error
function bar() {}

foo(); //This **WILL** throw an error
var foo = function() {}

[emphasis mine -r]

It really is worth reading all of the answer there, but it's also worth emphasizing that this JSLint error isn't just about style, it's warning you about the possibility of a functional error. Edge-case-y, sure, but a useful habit.

I'll also add that there shouldn't be a case where you have to recursively call functions in JavaScript that exist before they're defined. I've been annoyed when I've seen this error in that context a few times, but it's [almost?] always helpfully shown some code smell where a refactoring was useful rather than a place where all the function jumping is required.

It seems like you might be able to cheat around the warning if you jump a lot with function namespacing, which I may have embarrassingly done in a case or two. I hope not (on both counts), though.


* I was tempted to add JSLint to that question and call this one a dupe, but wasn't sure that was quite kosher.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744970732a277468.html

最新回复(0)