JSLint error "out of sight" due to ordering functions?

JSLint seems to be picky about ordering functions.

This goes fine:

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

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

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

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

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

Is it for design? I do not care? How can it be avoided in larger (more complex) cases when it is not always possible to give functions a clear order?

+4
source share
2 answers

JSLint / JSHint expects you to define functions before referencing them. However, JavaScript does not care because functions and variables are raised .

linter, , http://jshint.com/docs/options/#latedef

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

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

. fooobar.com/questions/79949/...

+4

@epascarello, JSHint, , .

, JSLint. *

: .. - , : function name() {}, , var name = function() {} ( , , ), .

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

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

[ -r]

, , JSLint , . Edge-case-y, , .

, , JavaScript, . , , [?] , , , .

, , , , , . , ( ).


* JSLint , , .

+1

Source: https://habr.com/ru/post/1622929/


All Articles