javascript jshint 了解 Angular 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26288367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does jshint understand Angular?
提问by David JM
jshint is throwing an error when defining an angular module (or directive, or factory) as recommended by the Angular style guides (by John Papaor Todd Motto). For example, for a controller like this:
jshint 在定义 Angular 风格指南(由John Papa或Todd Motto编写)推荐的 angular 模块(或指令或工厂)时抛出错误。例如,对于这样的控制器:
(function () {
'use strict';
angular
.module('myApp')
.controller('myAppCtrl', theController);
function theController() {...}
})();
... jshint throws this error:
... jshint 抛出此错误:
'theController' was used before it was defined.
The angular app works perfectly despite these errors. However I don't know why jshint protests...
尽管有这些错误,angular 应用程序仍能完美运行。但是我不知道为什么 jshint 抗议......
What am I missing? I wonder if jshint is a good evaluator of the quality of the angular code (despite it is included with popular packages as generator-angular) or it's me that I am doing something wrong (although my app works).
我错过了什么?我想知道 jshint 是否是 angular 代码质量的一个很好的评估器(尽管它作为生成器 angular 包含在流行的包中)或者是我做错了什么(尽管我的应用程序有效)。
Thanks in advance!
提前致谢!
回答by John Papa
Use the latedef
property and set it to false
. This allows hoisting of functions, which IMO is fine. But still reports hoisting of vars, which is bad IMO
使用该latedef
属性并将其设置为false
. 这允许提升功能,IMO 很好。但仍然报告吊装 vars,这是不好的 IMO
回答by Hyman.the.ripper
well first of include angular in your "globals variables", for example:
首先在“全局变量”中包含 angular,例如:
"globals": { // Global variables.
"jasmine": true,
"angular": true,
"browser": true,
"element": true,
"by":true,
"io":true,
"_":false,
"$":false
}
then move your function definition before you reference it from angular.
然后在从 angular 引用它之前移动你的函数定义。
(function () {
'use strict';
function theController() {...}
angular
.module('myApp')
.controller('myAppCtrl', theController);
})();
回答by ruffin
So another option, that makes every linter happy, is to declare the variable that'll hold the function first, use it as a param, and then define it.
所以另一个让每个 linter 满意的选择是首先声明将保存函数的变量,将其用作参数,然后定义它。
But personally I'm not sure I love the flow here. I think I like Hyman's answer better, but this is a little closer to what Papa seems to prefer, if you're down with his style guide. Actually, I'm not sure why this isn't what he recommends, if he really wants functions to appear after they're used (and he does). Otherwise, you can'tuse his style with latedef
set to true in JSHint -- or JSLint at all.
但就我个人而言,我不确定我是否喜欢这里的流动。我想我更喜欢 Hyman 的回答,但如果你不喜欢他的风格指南,这更接近爸爸似乎更喜欢的答案。实际上,我不确定为什么这不是他推荐的,如果他真的希望函数在使用后出现(他确实如此)。否则,您不能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";
};
}());
回答by Ryan Wheale
Your code shouldwork, but jshint is going to try and get you to code in a very strict manner. At the very least it's a "good practice" to have your functions defined before you use them. As I mentioned in the comment above, I think older javascript engines execute from top to bottom (can't remember for sure though and can't test) - so if you're going for wide-as-possible support you will want to listen to jshint.
您的代码应该可以工作,但是 jshint 会尝试让您以非常严格的方式进行编码。至少在使用函数之前先定义函数是一个“好习惯”。正如我在上面的评论中提到的,我认为旧的 javascript 引擎从上到下执行(虽然记不清并且无法测试) - 所以如果你想要尽可能广泛的支持,你会想要听听 jshint。
Something worth noting here is that if you use the var
keyword to define your function, you will get an error - best explained by example:
这里值得注意的是,如果你使用var
关键字来定义你的函数,你会得到一个错误——最好通过例子来解释:
This works (http://jsfiddle.net/ryanwheale/kr8L825p/)
这有效(http://jsfiddle.net/ryanwheale/kr8L825p/)
(function() {
try {
foo();
} catch(ex) {
alert("ahhhhh, what's going on?!?!?\n\n" + ex.message);
}
function foo() {
alert("I was hoisted to the top of this scope before execution :)");
}
})();
... but this doesn't (http://jsfiddle.net/ryanwheale/kr8L825p/4/)
...但这没有(http://jsfiddle.net/ryanwheale/kr8L825p/4/)
(function() {
try {
foo();
} catch(ex) {
alert("ahhhhh, what's going on?!?!?\n\n" + ex.message);
}
var foo = function() {
alert("I was hoisted to the top of this scope before execution :)");
}
})();