javascript 这个函数的语句太多了。(41)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31038080/
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
This function has too many statements. (41)
提问by Non
I have this controller
我有这个控制器
.controller('ctrl', function($scope, $rootScope, $timeout, $alert,
$location, $tooltip, $popover, BetSlipFactory,
AccordionsFactory, AuthFactory,
RiskWinCalculations) {...});
and, I am getting this error due to jshint
:
而且,由于以下原因,我收到此错误jshint
:
line 10 col 44 This function has too many statements. (41)
line 10 col 44 这个函数的语句太多了。(41)
so, what should I do to avoid it ?
那么,我该怎么做才能避免呢?
回答by Reacting
It doesn't mean poorly managed code as @pankajparkar says before, it could be because you have something like this, lets say this from one of my projects:
这并不意味着@pankajparkar 之前所说的代码管理不善,这可能是因为你有这样的东西,让我们从我的一个项目中说出来:
$scope.betLoader = false;
$scope.showIfbetAlerts = true;
$scope.displayStraight = true;
$scope.displayParlay = true;
$scope.displayIfBet = true;
$scope.displayTeaser = true;
$scope.displayPleaser = true;
$scope.displayReverse = true;
$scope.unavailableBet = false;
$scope.subAccordion = false;
$scope.betTypeShow = false;
$scope.showStraight = true;
you can do this:
你可以这样做:
$scope.setInitialState = function() {
$scope.betLoader = false;
$scope.showIfbetAlerts = true;
$scope.displayStraight = true;
$scope.displayParlay = true;
$scope.displayIfBet = true;
$scope.displayTeaser = true;
$scope.displayPleaser = true;
$scope.displayReverse = true;
$scope.unavailableBet = false;
$scope.subAccordion = false;
$scope.betTypeShow = false;
};
$scope.setInitialState();
that will fix it.
那将解决它。
UPDATE
更新
Let me explain:
让我解释:
it is not only related with the dependencies, jslint throws this error when there are too many statements, he says before on line ten which is where the controller begins, so parting from there, he should have too many statements, if you put all those statements in 1 function, those statements will be reduce to 1 :)
不仅和依赖有关,jslint在语句太多的时候会抛出这个错误,他之前在第10行是控制器开始的地方,所以从那里分开,他应该有太多的语句,如果你把所有那些1 个函数中的语句,这些语句将减少为 1 :)
回答by nicholas
The best way to get rid of the error would be to edit your jshint
settings to not display it.
摆脱错误的最佳方法是编辑您的jshint
设置以不显示它。
http://jshint.com/docs/options/#maxstatements
http://jshint.com/docs/options/#maxstatements
That's a very wishy-washy jshint
warning that doesn't really mean anything.
这是一个非常空洞的jshint
警告,实际上没有任何意义。
Normally, a function that requires more than 4 or 5 parameters is a bad idea for a lot of reasons, but isn't technically wrong. In this case those params are Angular's way of defining dependencies, so shouldn't be a problem. If the code works, I wouldn't worry about it.
通常,出于多种原因,需要超过 4 或 5 个参数的函数是一个坏主意,但在技术上并没有错。在这种情况下,这些参数是 Angular 定义依赖项的方式,所以应该不成问题。如果代码有效,我就不会担心。
回答by kamal pandey
If the controller wants more statements and you don't have any other method to remove it then go to your .jshintrc file and edit it like
如果控制器需要更多语句并且您没有任何其他方法可以删除它,那么转到您的 .jshintrc 文件并像这样编辑它
"maxstatements": 80, // or whatever number you want'
thanks
谢谢