Fork me on GitHub

JSLint Error Explanations

JSLint will hurt your feelings. It's time to make them better!


Unnecessary use strict

什么时候会产生这个错误?

JSLint在遇到"use strict"语句出现在一段已经运行在严格模式下的代码中时,会抛出"Unnecessary use strict"的错误。在下面这个例子中,我们将一个"use strict"语句放在全局作用域中,另一个放在函数中:

为什么会产生这个错误?

抛出这个错误是为了强调完全没有目的的代码片段"use strict"语句在其出现的作用域中起作用,当然也包括其后裔的可执行上下文。以下是ECMAScript 5规范中关于严格模式和函数的内容:(section §10.1.1):

Function code that is part of a FunctionDeclaration, FunctionExpression, or accessor PropertyAssignment is strict function code if its FunctionDeclaration, FunctionExpression, or PropertyAssignment is contained in strict mode code or if the function code begins with a Directive Prologue that contains a Use Strict Directive.

如果JSLint抛出了这个错误,你可以安全地讲高亮的"use strict"语句删除。


comments powered by Disqus