jshint “use strict” issue [duplicate]

让人想犯罪 __ 提交于 2019-11-27 21:33:27

问题


This question already has an answer here:

  • JSLint is suddenly reporting: Use the function form of “use strict” 8 answers

Here's my file: app/scripts/controllers/main.js

"use strict";

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

My Gruntfile.coffee has:

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"]

When I run grunt, I get:

Linting app/scripts/controllers/main.js ...ERROR
[L1:C1] W097: Use the function form of "use strict".
"use strict";

回答1:


The issue is that if you don't use the function form it applies to everything, and not just your code. The solution to that is to scope use strict inside functions you control.

Refer to this question: JSLint is suddenly reporting: Use the function form of “use strict”.

Rather than doing

"use strict";

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

You should be doing

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

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

It's either that or wrapping your code in a self-executing closure, like below.

(function(){
    "use strict";

    // your stuff
})();



回答2:


Changed my Gruntfile.coffee to include globalstrict

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


来源:https://stackoverflow.com/questions/19910134/jshint-use-strict-issue

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