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 this might have to do with "hoisting" as explained here: JavaScript function order: why does it matter?
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 code snippet should not return an error. Am I mistaken?
Even considering the hoisting mechanism, I still do not understand why the first code snippet should be wrong. Function a
is only called after function b
is defined, so b
would be in a
's closure. Is my code wrong or is jsHint wrong?
I understand that this question is purely academic, because the code works as expected in all browser. Nevertheless, I'd like to know why jsHint throws an error.