如何使用JSLint全局设置'use strict' [英] How to set 'use strict' globally with JSLint
问题描述
我是javascript的新手,正在尝试通过JSLint进行验证。
我应该在哪里放使用严格来全局使用并验证?
I'm new to javascript and am trying to validate through JSLint. Where should I put "use strict" to use it globally and validate?
这给了我错误在语句位置使用严格'的意外表达式。:
This gives me error "Unexpected expression 'use strict' in statement position.":
"use strict";
console.log('doing js in head-section');
function helloWorld() {
console.log('called function helloWorld()');
alert('Hello World from a JS function showing an alert!');
}
function helloMyNumber() {
console.log('called function helloMyNumber()');
var max = 42;
var yourLuckyNumber = prompt('Enter your lucky number (between 1 and '+ max +')');
var myLuckyNumber = Math.floor(Math.random()*(max+1));
var paragraph = document.getElementById('luckynumber');
paragraph.innerHTML = paragraph.innerHTML + ' Your lucky number is: ' + yourLuckyNumber + '. Mine is: ' + myLuckyNumber + '. They ' + (yourLuckyNumber == myLuckyNumber ? 'DID ' : 'did NOT ') + 'match!';
}
console.log('doing JS in body-section');
document.writeln('<p class="green">Hello World from JS within a body-section in HTML!</p>');
推荐答案
根据文档,JSLint的浏览器
选项会自动禁用在全球范围内使用严格的;
。据我所知,没有办法重新打开它。
According to the documentation, the browser
option for JSLint automatically disables use of "use strict";
at the global level. As far as I'm aware, there's no way to turn it back on.
您可以关闭浏览器选项,并获得与<$相同的预先声明的全局变量c $ c>浏览器选项使用:
You could turn off the browser option, and get the same predeclared globals as the browser
option by using:
/*global
Audio, clearInterval, clearTimeout, document, event, history, Image, location, name, navigator, Option, screen, setInterval, setTimeout, XMLHttpRequest
*/
或者,您可以将所有代码包装在IIFE中,并在其顶部使用use strict;
。
Alternately you could wrap all of your code in an IIFE and use "use strict";
at the top of it.
或者,您可以切换到 JSHint (有更多选项),以及使用 strict:global
选项在全局范围内允许use strict;
。
Alternately you could switch to JSHint (has more options), and use its strict: global
option to allow "use strict";
at global scope.
这篇关于如何使用JSLint全局设置'use strict'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!