jshint“使用严格”的问题

时间:2013-11-11 15:37:32

标签: jshint

这是我的档案: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";

2 个答案:

答案 0 :(得分:49)

问题在于,如果您不使用函数表单,它将适用于所有内容,而不仅仅是您的代码。解决方法是将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
})();

答案 1 :(得分:8)

将我的Gruntfile.coffee更改为包含globalstrict

jshint:
    options:
        globalstrict: true
        globals:
            require: false
            module: false
            console: false
            __dirname: false
            process: false
            exports: false