jshint“使用严格"问题 [英] jshint "use strict" issue

查看:53
本文介绍了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";

推荐答案

问题是,如果您不使用函数形式,则它不仅适用于您的代码,还适用于所有内容.解决方案是将use strict限制在您控制的函数之内.

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.

请参阅以下问题: JSLint突然报告:使用使用严格"的功能形式..

而不是做

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

就是这样,或者将您的代码包装在一个自动执行的闭包中,如下所示.

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

(function(){
    "use strict";

    // your stuff
})();

这篇关于jshint“使用严格"问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆