JavaScript for循环中的i ++与++ i [英] i++ vs. ++i in a JavaScript for loop
问题描述
由于JSLint,我几乎总是使用i += 1
来递增JavaScript for循环,但是对于快速而肮脏的脚本,我改用i++
.
但是,我在其他人的代码中看到很多for循环,它们通过执行++i
来使i
递增.
Because of JSLint, I almost always use i += 1
to increment a JavaScript for loop, but for quick and dirty scripts, I use i++
instead.
However, I see a lot of for loops in other people's code in which they increment i
by doing ++i
instead.
据我所知,i++
和++i
之间的含义没有差异,并且jsPref在性能上没有差异.
因此,我想知道++i
的惯例来自何处以及人们为什么倾向于这样做.
As far as I know, there is no difference in meaning between i++
and ++i
, and jsPref shows no difference in performance.
As such, I'm wondering where the convention of doing ++i
comes from and why people tend to do it.
有人知道为什么在for循环中增加计数器时为什么许多JS编码器倾向于++i
而不是i++
吗?
谢谢.
Does anyone know why a lot of JS coders tend to prefer ++i
over i++
when incrementing the counter in a for loop?
Thanks.
推荐答案
在JS和PHP中没有任何区别,我认为即使在Java中也没有任何区别,但在纯c语言中,当编译器未优化代码时,确实如此,这就是为什么很多人使用++ i的原因,因为他们习惯于c语言.
In JS and PHP it does not make any difference, I think even in Java it does not make any difference but in pure c when compiler is not optimizing code it does, and that is why a lot of people use ++i because they are used to it from c.
如果您想知道prerc和post post增量的历史记录searc C/C ++ pre/post增量,这是JS的答案.或查看有关@Orvev答案的评论.
This is an answer for JS if you want history of pre and post increment searc C/C++ pre/post increment. Or see comments on @Orvev's answer.
这篇关于JavaScript for循环中的i ++与++ i的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!