【发布时间】:2011-12-27 18:21:48
【问题描述】:
Jshint.com 报错:
第 36 行:var signin_found;缺少“use strict”语句。
【问题讨论】:
-
您可以通过选中选项
Tolerate missing 'use strict' pragma(右栏)来忽略此警告。
标签: javascript jshint
Jshint.com 报错:
第 36 行:var signin_found;缺少“use strict”语句。
【问题讨论】:
Tolerate missing 'use strict' pragma(右栏)来忽略此警告。
标签: javascript jshint
在 js 文件的顶部(在 .js 文件的第 1 行)添加“use strict”:
"use strict";
...
function initialize_page()
{
var signin_found;
/*Used to determine which page is loaded / reloaded*/
signin_found=document.getElementById('signin_button');
if(signin_found)
{
有关“使用严格”的更多信息,请参见 stackoverflow 上的另一个问题:
What does "use strict" do in JavaScript, and what is the reasoning behind it?
更新。
jshint.com 出了点问题,它要求你在每个函数中加上“use strict”,但应该允许为每个文件全局设置它。
jshint.com 认为这是错误的。
"use strict";
function asd()
{
}
不过也没什么不好……
它希望你对每个函数都加上“use strict”:
function asd()
{
"use strict";
}
function blabla()
{
"use strict";
}
然后它说:
干得好! JSHint 没有发现你的代码有任何问题。
【讨论】:
这里是 JSHint 维护者。
JSHint(网站上使用的版本)要求您在代码中使用函数级严格模式。关闭它很容易,您只需取消选中“代码未处于严格模式时发出警告”复选框:
为什么我们不允许@Czarek 建议的全局严格模式?因为您页面上使用的某些 JavaScript 文件可能与严格模式不兼容,并且全局严格模式会破坏该代码。要使用全局严格模式,有一个名为 globalstrict 的选项。
希望有帮助!
【讨论】:
/*jshint */ 构造设置它们。基本上,我不希望 jshint.com 的首页被无数选项吓倒。
我认为这是因为 jshint 试图“保护”我们免受意外分配严格模式对整个文件的影响。 并且最好用匿名函数包装代码,或者使用某种命名空间。
例如两者都在严格模式下运行:
(function() {
"use strict";
function foo() {
.....
}
function bar() {
.....
}
}());
【讨论】:
JSlint 要求您的代码处于“严格模式”
为此,只需将"use strict"; 添加到代码顶部即可。
【讨论】:
"use strict"; ECMAScript 5 把它解释为一个函数。 ECMAScript 3 直接忽略它。
(function(){ "use strict"; //rest of your code }());