admin管理员组文章数量:1241085
I am well aware of similar questions, but I still can't solve mine basing on those.
So, my code looks like this:
for (var e, i = 0; e = formErrors[i]; i += 1)
JSHint returns said error in char 39, so with ;
after formErrors[i]
. What can I do?
I am well aware of similar questions, but I still can't solve mine basing on those.
So, my code looks like this:
for (var e, i = 0; e = formErrors[i]; i += 1)
JSHint returns said error in char 39, so with ;
after formErrors[i]
. What can I do?
4 Answers
Reset to default 10JSHint is warning you of a potential bug. It is expected that the 2nd part of a for
statement will be a Boolean expression. Normally, you'd use one of the parison operators for this (==
, ===
, !=
, >
etc..). Since the expression is e = formErrors[i]
it looks like it might be a mistake, possibly due to a missing equal sign. This is a mon typo that causes a lot of bugs.
Apparently, in this case it is not a bug, but rather an intentional usage of the fact that any expression evaluates to something, and an assignment expression evaluates to the assigned value:
var x;
alert(x = 1);
So the for
statement actually assigns a new value to e
, but also evaluates that value as a condition, casting it to Boolean if required.
You could refactor your code such that it both assigns the value and uses a casting operation that would satisfy JSHint and make the code a bit more obvious to the reader:
for (var e, i = 0; !!(e = formErrors[i]); i += 1)
The 2nd !
(the one directly in front of (e...
) causes a casting to Boolean, but also negates it, the 1st !
reverts this negation.
That's just a very weird way of writing a loop. JsHint expects a boolean expression ("conditional") there, and judges that your assignment is a mistake and you actually wanted a parison (==
instead of =
).
What you should do is switch to the following mon array iteration idiom:
for (var i = 0; i < formErrors.length; i += 1) {
var e = formErrors[i];
…
(Which works the same as your original code for non-sparse formErrors
arrays that contain no falsy values.)
Or alternatively, if you want to write non-idiomatic code, dump jshint :-)
e = formErrors[i] is an assignment, don't you want a condition there? use a ma after i=0 in that case, else put a condition after the semicolon.
Typically, the middle element in a for loop is the condition that is used to decide whether to continue the loop or not. You have an assignment there, if you wanted a condition you should use e === formErrors[i]
(or with a double =
, but that is usually not remended).
An assignment can technically work, because e can be, for example, some object (true) or null (false). But that is considered bad coding style, usually, and won't make for very readable code.
本文标签: javascriptJSHint Expected a conditional expression and instead saw an assignmentStack Overflow
版权声明:本文标题:javascript - JSHint -Expected a conditional expression and instead saw an assignment - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740054750a2222312.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论