由于函数排序导致 JSLint "out of scope" 错误?
JSLint "out of scope" error due to function ordering?
JSLint 似乎对函数排序很挑剔。
这很好:
function a() {
'use strict';
return 1;
}
function b() {
'use strict';
a();
}
虽然这给出了 'a' is out of scope
错误消息:
function b() {
'use strict';
a();
}
function a() {
'use strict';
return 1;
}
这是设计使然吗?我应该关心吗?在更大(更复杂)的情况下如何避免它可能并不总是能够给函数一个明确的顺序?
JSLint/JSHint 希望您在引用函数之前定义它们。但是,JavaScript 并不关心,因为 functions and variables are hoisted。
您可以更改代码风格,或使用 http://jshint.com/docs/options/#latedef
告诉 linter 忽略它
/* jshint latedef:nofunc */
function b() {
'use strict';
a();
}
function a() {
'use strict';
return 1;
}
见
to where this was discussed for JSHint 确实是绝对重要的,因为这不仅仅是风格的问题。
让我们达到 the excellent answer at that question 的最高点,因为它也适用于 JSLint。*
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]
确实是 worth reading all of the answer there,但同样值得强调的是 这个 JSLint 错误不仅仅与样式有关,它还警告您可能存在功能错误。 Edge-case-y,当然,但是一个有用的习惯。
我还要补充一点,在 JavaScript 中不存在必须递归调用定义之前就存在的函数的情况。当我在那种情况下多次看到这个错误时,我感到很恼火,但它 [几乎?] 总是有助于显示一些代码味道,重构是有用的,而不是所有函数跳转的地方 需要。
如果您使用函数命名空间跳转很多,似乎您可以绕过警告,我可能在一两个案例中尴尬地做到了。不过,我希望不会(在这两个方面)。
* 我很想在那个问题中添加 JSLint 并将其称为一个骗局,但不确定这是否完全正确。
JSLint 似乎对函数排序很挑剔。
这很好:
function a() {
'use strict';
return 1;
}
function b() {
'use strict';
a();
}
虽然这给出了 'a' is out of scope
错误消息:
function b() {
'use strict';
a();
}
function a() {
'use strict';
return 1;
}
这是设计使然吗?我应该关心吗?在更大(更复杂)的情况下如何避免它可能并不总是能够给函数一个明确的顺序?
JSLint/JSHint 希望您在引用函数之前定义它们。但是,JavaScript 并不关心,因为 functions and variables are hoisted。
您可以更改代码风格,或使用 http://jshint.com/docs/options/#latedef
告诉 linter 忽略它/* jshint latedef:nofunc */
function b() {
'use strict';
a();
}
function a() {
'use strict';
return 1;
}
见
让我们达到 the excellent answer at that question 的最高点,因为它也适用于 JSLint。*
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 likevar 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]
确实是 worth reading all of the answer there,但同样值得强调的是 这个 JSLint 错误不仅仅与样式有关,它还警告您可能存在功能错误。 Edge-case-y,当然,但是一个有用的习惯。
我还要补充一点,在 JavaScript 中不存在必须递归调用定义之前就存在的函数的情况。当我在那种情况下多次看到这个错误时,我感到很恼火,但它 [几乎?] 总是有助于显示一些代码味道,重构是有用的,而不是所有函数跳转的地方 需要。
如果您使用函数命名空间跳转很多,似乎您可以绕过警告,我可能在一两个案例中尴尬地做到了。不过,我希望不会(在这两个方面)。
* 我很想在那个问题中添加 JSLint 并将其称为一个骗局,但不确定这是否完全正确。