小编Bru*_*eur的帖子

在条件中分配表达是一种不好的做法?

这篇文章解释了为什么我使用这样的代码时会发出警告:

var htmlCollection = document.getElementsByClassName("class-name"),
    i = htmlCollection.length,
    htmlElement;

// Because htmlCollection is Live, we use a reverse iteration.
while (htmlElement = htmlCollection[--i]) { // **Warning?! Why?!**
    htmlElement.classList.remove("class-name");
}
Run Code Online (Sandbox Code Playgroud)

但这并没有解释为什么在一段时间内分配表达式是一种不好的做法?».

我也读过这个stackoverflow的答案,指出这种做法是好的.所以...

while (element = element.parentNode)类似语法的性能问题还是样式代码推荐?


顺便说一句,似乎«--i»运算符也是一种不好的做法.我在这篇文章中读到:

已知++(递增)和 - (递减)运算符通过鼓励过多的诡计来导致错误的代码.

这是某种玩笑?

javascript while-loop assignment-operator

2
推荐指数
1
解决办法
408
查看次数