jshint "use strict"问题

标签 jshint

这个问题在这里已经有了答案:





JSLint is suddenly reporting: Use the function form of "use strict"

(9 个回答)


7年前关闭。




这是我的文件:app/scripts/controllers/main.js

"use strict";

angular.module('appApp')
  .controller('MainCtrl', ['$scope', function ($scope) {
    $scope.awesomeThings = [
      'HTML5 Boilerplate',
      'AngularJS',
      'Karma'
    ];
  }]);

我的 Gruntfile.coffee已:
jshint:
    options:
        globals:
            require: false
            module: false
            console: false
            __dirname: false
            process: false
            exports: false

    server:
        options:
            node: true
        src: ["server/**/*.js"]

    app:
        options:
            globals:
                angular: true
                strict: true

        src: ["app/scripts/**/*.js"]

当我跑 grunt ,我得到:
Linting app/scripts/controllers/main.js ...ERROR
[L1:C1] W097: Use the function form of "use strict".
"use strict";

最佳答案

问题是,如果您不使用函数形式,它适用于所有内容,而不仅仅是您的代码。解决方案是范围 use strict您控制的内部功能。

引用这个问题:JSLint is suddenly reporting: Use the function form of “use strict” .

而不是做

"use strict";

angular.module('appApp')
  .controller('MainCtrl', ['$scope', function ($scope) {
    $scope.awesomeThings = [
      'HTML5 Boilerplate',
      'AngularJS',
      'Karma'
    ];
  }]);

你应该做
angular.module('appApp')
  .controller('MainCtrl', ['$scope', function ($scope) {
    "use strict";

    $scope.awesomeThings = [
      'HTML5 Boilerplate',
      'AngularJS',
      'Karma'
    ];
  }]);

要么就是这样,要么将您的代码包装在一个自动执行的闭包中,如下所示。
(function(){
    "use strict";

    // your stuff
})();

关于jshint "use strict"问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19910134/

相关文章:

函数的 Javascript JQuery 最佳实践

eclipse-plugin - "File is not a valid JSHint library"尝试将最新版本的 jshint 与 eclipse 插件 (Titanium) 一起使用时

javascript - JSHint 的 "function declarations should not be placed in blocks"太宽泛了吗?

node.js - npm install -g <package> 错误 EADDRINUSE

gulp-jshint : How to fail the build?

javascript - 如何抑制 "{variable} is better written in dot notation."

javascript - JSHint 是否支持异步/等待?

javascript - Gulp:在单个文件更改时运行 jshint

javascript - 如何将 fixmyjs 与 EsLint 一起使用?

javascript - Gulp:带参数的 Jsc