【问题标题】:How to set 'use strict' globally with JSLint如何使用 JSLint 全局设置“使用严格”
【发布时间】:2016-05-19 18:10:59
【问题描述】:

我是 javascript 新手,正在尝试通过 JSLint 进行验证。 我应该在哪里放置“使用严格”以在全球范围内使用它并进行验证?

这给了我错误“语句位置出现意外的表达式'use 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>');

【问题讨论】:

    标签: javascript jslint use-strict


    【解决方案1】:

    根据the documentation,JSLint 的browser 选项会在全局级别自动禁用"use strict";。据我所知,无法重新开启。

    您可以关闭浏览器选项,并使用以下方法获得与 browser 选项相同的预声明全局变量:

    /*global
    Audio, clearInterval, clearTimeout, document, event, history, Image, location, name, navigator, Option, screen, setInterval, setTimeout, XMLHttpRequest
    */
    

    或者,您可以将所有代码包装在 IIFE 中,并在其顶部使用 "use strict";

    或者,您可以切换到JSHint(有更多选项),并使用其strict: global 选项在全局范围内允许"use strict";

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • "use strict"; 在文件开头完全有效。
      猜你喜欢
      • 2013-11-17
      • 1970-01-01
      • 2021-06-11
      • 1970-01-01
      • 2020-10-24
      • 1970-01-01
      • 2012-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多