Dav*_* JM 24 javascript jshint angularjs
根据Angular样式指南(John Papa或Todd Motto)的建议,jshint在定义角度模块(或指令或工厂)时会抛出错误.例如,对于像这样的控制器:
(function () {
'use strict';
angular
.module('myApp')
.controller('myAppCtrl', theController);
function theController() {...}
})();
Run Code Online (Sandbox Code Playgroud)
... jshint抛出此错误:
'theController' was used before it was defined.
Run Code Online (Sandbox Code Playgroud)
尽管存在这些错误,角度应用程序仍可正 但是我不知道为什么jshint抗议......
我错过了什么?我想知道jshint是否是角度代码质量的一个很好的评估者(尽管它包含在流行的包中作为生成器角度)或者我是我做错了(虽然我的应用程序工作).
提前致谢!
Joh*_*apa 18
使用该latedef属性并将其设置为false.这允许提升功能,IMO很好.但仍然报告悬挂变量,这是不好的IMO
jac*_*per 15
首先在你的"全局变量"中包含angular,例如:
"globals": { // Global variables.
"jasmine": true,
"angular": true,
"browser": true,
"element": true,
"by":true,
"io":true,
"_":false,
"$":false
}
Run Code Online (Sandbox Code Playgroud)
然后在从角度引用它之前移动函数定义.
(function () {
'use strict';
function theController() {...}
angular
.module('myApp')
.controller('myAppCtrl', theController);
})();
Run Code Online (Sandbox Code Playgroud)
因此,使每个linter满意的另一个选择是声明首先保存函数的变量,将其用作参数,然后定义它.
但就个人而言,我不确定我喜欢这里的流程.我觉得我更喜欢杰克的答案,但如果你的风格指南低迷的话,这会更接近爸爸似乎更喜欢的东西.实际上,我不确定为什么这不是他推荐的,如果他真的希望功能在他们被使用后出现(他确实如此).否则,你不能latedef在JSHint 中使用他的样式设置为true - 或者根本不使用JSLint .
(function () {
'use strict';
var theController;
angular
.module('myApp')
.controller('myAppCtrl', theController);
theController = function () {
return "so jslint doesn't complain about empty blocks";
};
}());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16580 次 |
| 最近记录: |