JavaScript for循环中的i ++与++ i [英] i++ vs. ++i in a JavaScript for loop

查看:193
本文介绍了JavaScript for循环中的i ++与++ i的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆