如何使用 JSLint 全局设置“使用严格”
How to set 'use strict' globally with JSLint
我是javascript的新手,正在尝试通过JSLint进行验证。我应该在哪里放置"使用严格"以全局使用它并进行验证?
这给了我错误"语句位置中意外的表达式'使用 strict'。
"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 的browser
选项会自动禁用全局级别"use strict";
的使用。据我所知,没有办法重新打开它。
您可以关闭浏览器选项,并使用以下命令获取与browser
选项相同的预声明全局变量:
/*global
Audio, clearInterval, clearTimeout, document, event, history, Image, location, name, navigator, Option, screen, setInterval, setTimeout, XMLHttpRequest
*/
或者,您可以将所有代码包装在 IIFE 中,并在 IIFE 顶部使用"use strict";
。
或者,您可以切换到JSHint(有更多选项(,并使用其strict: global
选项允许全局范围内的"use strict";
。
">
use strict"通常用于函数的开头。对于您的代码,我只是将其全部包装在 IIFE 中,这将使"使用严格"有效
(function() {
"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>');
})();
相关文章:
- 如果我使用或不使用“;使用严格的“;JS中的模式
- 使用严格不允许在节点.js中使用它
- 带有RequireJS的AngularJS应用程序在Safari和Firefox中给出了“使用严格”错误
- 当使用严格的DOCTyPE定义时,jQuery可拖动性得到扩展
- “使用严格”对于旧版浏览器
- 使用“使用严格”作为“使用强”的备份
- 仅在调试中使用“严格”
- 为什么“使用严格”仍然是一个字符串字面
- 如何使用 JSLint 全局设置“使用严格”
- 严格模式如何(“使用严格;)由函数继承
- 使用“使用严格”,但仍收到警告
- YUI压缩器和“使用严格”的提示
- 自定义“使用严格”类似指令
- 如何在 javascript 中使用严格模式发送控制字符
- “使用严格”和下划线.js的问题
- 缺少“使用严格”语句
- 使用严格:如何测试它
- 缺少“;使用严格的“;即使它's已在文件顶部声明
- 我可以在angularjs中使用严格的过滤器来搜索所有页面吗
- 将“;使用严格的“;在Firefox扩展中,它会停止运行,但不会显示任何错误