How to set 'use strict' globally with JSLint

大憨熊 提交于 2019-12-19 05:44:50

问题


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>');

回答1:


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.

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
*/

Alternately you could wrap all of your code in an IIFE and use "use strict"; at the top of it.

Alternately you could switch to JSHint (has more options), and use its strict: global option to allow "use strict"; at global scope.




回答2:


'use strict' is typically used at the start of functions. For your code, I would just wrap it all in an IIFE, which would make 'use strict' valid

(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>');
})();


来源:https://stackoverflow.com/questions/35297461/how-to-set-use-strict-globally-with-jslint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!