【问题标题】:jslint error : Unexpected expression 'use strict' in statement position [duplicate]jslint 错误:语句位置出现意外的表达式“使用严格”[重复]
【发布时间】:2017-05-11 23:48:16
【问题描述】:

当我尝试将以下代码保存在 sublime text 中时,

'use strict';
 /*global angular,_*/

 var app = angular.module("myApp", []);
 app.controller("myCtrl", function ($scope) {
   $scope.firstName = "John";
   $scope.lastName = "Doe";
 });

我收到以下 jslint 错误:

 #1 Unexpected expression 'use strict' in statement position.
    'use strict'; // Line 1, Pos 1
 #2 Place the '/*global*/' directive before the first statement.
    /*global angular,_*/ // Line 2, Pos 1
 #3 Undeclared 'angular'.
    var app = angular.module("myApp", []); // Line 4, Pos 11
 #4 Expected 'use strict' before '$scope'.
    $scope.firstName = "John"; // Line 6, Pos 3
 #5 Expected '$scope' at column 5, not column 3.
    $scope.lastName = "Doe"; // Line 7, Pos 3

【问题讨论】:

  • 我已经检查了该链接 @TheSharpieOne 。这不符合我的目的。
  • So.. 答案的一部分说“根据the documentation,JSLint 的浏览器选项会自动禁用“使用严格”;在全局级别。就我而言请注意,没有办法重新打开它。”没有帮助您意识到您不能将'use strict'; 放在那里并且放在那里有问题吗?
  • 如果 'use strict' 没有被使用,那么 jslint 错误仍然存​​在,说“预期 'use strict' 在 '$scope' 之前”

标签: javascript angularjs jslint


【解决方案1】:

你不能在 jslint 中以全局方式使用 'use strict';。见https://stackoverflow.com/a/35297691/1873485

考虑到这一点,您需要将其从全局范围中删除并将其添加到您的函数范围中,或者将所有内容包装在 IFEE 中

 /*global angular,_*/
 var app = angular.module("myApp", []);
 app.controller("myCtrl", function ($scope) {
  'use strict';
   $scope.firstName = "John";
   $scope.lastName = "Doe";
 });

或包装它:

/*global angular,_*/
(function(){
  'use strict';
   var app = angular.module("myApp", []);
   app.controller("myCtrl", function ($scope) {
     $scope.firstName = "John";
     $scope.lastName = "Doe";
   });
})();

【讨论】:

  • 是的,但不是。 JSLint 使用错误消息处理包装:“不要在括号中包装函数文字。”
猜你喜欢
  • 2015-07-27
  • 2018-05-04
  • 2012-08-13
  • 2012-10-21
  • 2011-03-01
  • 2018-10-06
  • 2014-05-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多