编程语言
首页 > 编程语言> > javascript-jslint错误:语句位置出现意外的表达式’use strict’

javascript-jslint错误:语句位置出现意外的表达式’use strict’

作者:互联网

这个问题已经在这里有了答案:            >            How to set ‘use strict’ globally with JSLint                                    2个
当我尝试将以下代码保存为精美文字时,

'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

解决方法:

您不能使用“使用严格”; jslint在全球范围内.查看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";
   });
})();

标签:angularjs,jslint,javascript
来源: https://codeday.me/bug/20191111/2023196.html