Javascript 为什么人们使用 i = i + 1 而不是 i++?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7313710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do people use i = i + 1 instead of i++?
提问by 0x499602D2
I've seen that in a number of loops and increments. Instead of doing i++ they do i += 1. Why is this?
我已经在许多循环和增量中看到了这一点。他们不做 i++,而是做 i += 1。这是为什么呢?
采纳答案by SoapBox
Not all languages have ++
operator (python, for one)... Probably these people come from a background in one of those languages. Also some people feel that i++
is not very clear, especially since some languages treat i++
and ++i
differently.
并非所有语言都有++
运算符(python,例如)......这些人可能来自其中一种语言的背景。也有人觉得i++
不是很清楚,特别是有些语言对待i++
和++i
区别对待。
回答by Daniel A. White
Personal preference and style.
个人喜好和风格。
回答by Randomblue
Prevents excess craftiness. At least that is what Crockford says.
防止过度狡猾。至少克罗克福德是这么说的。
回答by Akinos
The general reason is that there are two different versions of increment that behave differently
一般的原因是有两种不同版本的 increment 表现不同
var i = 0;
1 == ++i // true
and
和
var i = 0;
1 == i++; // false
++i
translates to "increment i, then evaluate" while i++
translates to "evaluate i, then increment"
++i
转换为“递增 i,然后评估”而i++
转换为“评估 i,然后递增”
When you write these expressions as i = i + 1;
it's clear what the intent of the programmer was and easier to find bugs in the code. It's the same reason people write "yoda clauses" like
当您编写这些表达式i = i + 1;
时,程序员的意图就很清楚了,并且更容易发现代码中的错误。这与人们写“尤达条款”的原因相同
if(6 == x){
//. . .
}
because if you accidentally do
因为如果你不小心做
if(6 = x){
//. . .
}
it's easier to catch the mistake
更容易发现错误
回答by Michael Bray
i = i + 1
is easier to decode in English. i++
although totally correct doesn't translate well when beginners are reading the code. The programmer was possibly trying to make their code more readable by beginners, or perhaps just not trying to be overly concerned about syntax. There's no goodreason to use one over the other.
i = i + 1
用英文更容易解码。 i++
尽管当初学者阅读代码时完全正确并不能很好地翻译。程序员可能试图让他们的代码对初学者更具可读性,或者只是不想过度关注语法。没有充分的理由使用一个而不是另一个。
回答by Guffa
Some people find i = i + 1
or i += 1
more descriptive than i++
. It's just a matter of coding style and readability. In most current languages there is no performance difference at all between the different ways.
有些人发现i = i + 1
或i += 1
比 更具描述性i++
。这只是编码风格和可读性的问题。在大多数当前语言中,不同方式之间根本没有性能差异。
There are some readability issues with the ++
and --
operators, but that's mostly when they are used along with other operators, it's not really a problem when they are used by themselves. An expression like ++x+-++y+-z++
for example is perfectly valid, but it's hard to see what it actually does. As the operators cause readability issues in some cases, some feel that they should always be avoided.
++
and--
运算符存在一些可读性问题,但这主要是当它们与其他运算符一起使用时,当它们单独使用时并不是真正的问题。像++x+-++y+-z++
example这样的表达式是完全有效的,但很难看出它实际上做了什么。由于运算符在某些情况下会导致可读性问题,因此有些人认为应该始终避免使用它们。