How to set 'use strict' globally with JSLint

前端 未结 2 1129
旧巷少年郎
旧巷少年郎 2021-01-12 01:48

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 \"Unexpecte

2条回答
  •  一整个雨季
    2021-01-12 02:36

    '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('

    Hello World from JS within a body-section in HTML!

    '); })();

提交回复
热议问题