JsHint function "function not defined", a question about the execution of functions?

Passing this code through jsHint:

var A = function (spec) { "use strict"; var a = function () { return b(); }; var b = function () { return 5; }; a(); }; 

returns this error:

 Line 4: return b(); 'b' is not defined. 

I understand that this may be related to "lifting" as described here: Order of JavaScript functions: why is this important?

However, the following code returns the same error:

 var A = function (spec) { "use strict"; function a () { return b(); } function b () { return 5; } a(); }; 

If I understand correctly, at least the second piece of code should not return an error. Am I mistaken?

Even given the lifting mechanism, I still don't understand why the first piece of code should be wrong. Function a is called only after function b defined, so b will be closed by a . Is my code incorrect or jsHint incorrect?

I understand that this question is purely academic in nature, because the code works as expected in all browsers. However, I would like to know why jsHint throws an error.

+4
source share
3 answers

This is a false value in jsLint.
Both pieces of code work fine.

Ignore warning.

+4
source

The first example is the lift problem, because () refers to b () before the declaration. The solution is to have "var a, b;" after "strict use"; statement.

+2
source

jsHint is trying to help you by identifying potential problems and unconventional code.

In this case, it is not happy, because it can be difficult for a person to understand, even if it is absolutely correct Javascript.

0
source

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


All Articles