为什么jshint不能将赋值识别为表达式?

19 javascript jshint

我如何修改这些行以使jshint满意?

赋值是一种表达.为什么jshint不理解这个?显然翻译是这样的.

Line 572: while(bookmark_element=bookmark_list[iterator++])

Expected a conditional expression and instead saw an assignment.


Line 582: while(bookmark_element=bookmark_list[iterator++])

Expected a conditional expression and instead saw an assignment.


Line 623: while(element_iterator=element_iterator.nextSibling)

Expected a conditional expression and instead saw an assignment.
Run Code Online (Sandbox Code Playgroud)

Rob*_*b W 20

如果你真的想听JSHint,请将表达式转换为布尔值:

while (!!(bookmark_element=bookmark_list[iterator++]))

! means: Something that evaluates to true is converted to false,
         something that evaluates to false is converted to true.
Run Code Online (Sandbox Code Playgroud)

所以,!!意思是:将某些东西转换为条件表示.

  • 你实际上不需要在这里强制转换为布尔值.在赋值周围添加额外括号足以让jshint将其识别为表达式.所以在上面的答案中,你可以删除!! 并且警告仍然是沉默的 (17认同)

Mar*_*ers 13

我确信jshint理解表达式很好,只是大多数人写的if (a = b)实际意味着if (a == b)所以这会产生警告.

由于您的代码是您想要的,您可以添加一个明确的测试:

while ((element_iterator = element_iterator.nextSibling) !== null) { ... }
Run Code Online (Sandbox Code Playgroud)