++someVariable 与 someVariable++在 JavaScript 中

发布于 2024-09-13 17:58:05 字数 114 浏览 7 评论 0原文

在 JavaScript 中,您可以在变量名称之前(预增量)或变量名称之后(后增量)使用 ++ 运算符。这些递增变量的方法之间有什么区别(如果有的话)?

In JavaScript you can use ++ operator before (pre-increment) or after the variable name (post-increment). What, if any, are the differences between these ways of incrementing a variable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

Saygoodbye 2024-09-20 17:58:05

与其他语言相同:

  • ++x(前自增)表示“变量自增;表达式的值就是最终值”
  • x++(后自增)意思是“记住原始值,然后递增变量;表达式的值是原始值”

现在,当用作独立语句时,它们的含义相同:

x++;
++x;

当您在其他地方使用表达式的值时,就会出现差异。例如:

x = 0;
y = array[x++]; // This will get array[0]

x = 0;
y = array[++x]; // This will get array[1]

Same as in other languages:

  • ++x (pre-increment) means "increment the variable; the value of the expression is the final value"
  • x++ (post-increment) means "remember the original value, then increment the variable; the value of the expression is the original value"

Now when used as a standalone statement, they mean the same thing:

x++;
++x;

The difference comes when you use the value of the expression elsewhere. For example:

x = 0;
y = array[x++]; // This will get array[0]

x = 0;
y = array[++x]; // This will get array[1]
清晨说晚安 2024-09-20 17:58:05
  • ++x 递增该值,然后计算并存储它。
  • x++ 计算该值,然后递增并存储它。
var n = 0, m = 0;

alert(n++); /* Shows 0, then stores n = 1 */
alert(++m); /* Shows 1, then stores m = 1 */

请注意,尽可能使用 ++x 会带来轻微的性能优势,因为您读取变量,修改它,然后评估并存储它。与 x++ 运算符不同,您可以在其中读取值、求值、修改它,然后存储它。

  • ++x increments the value, then evaluates and stores it.
  • x++ evaluates the value, then increments and stores it.
var n = 0, m = 0;

alert(n++); /* Shows 0, then stores n = 1 */
alert(++m); /* Shows 1, then stores m = 1 */

Note that there are slight performance benefits to using ++x where possible, because you read the variable, modify it, then evaluate and store it. Versus the x++ operator where you read the value, evaluate it, modify it, then store it.

雨夜星沙 2024-09-20 17:58:05

据我了解,如果你单独使用它们,它们会做同样的事情。如果您尝试将它们的结果输出为表达式,那么它们可能会有所不同。尝试将alert(i++) 与alert(++i) 进行比较,看看有什么不同。 i++ 在加法之前计算为 i,而 ++i 在计算之前先进行加法。

有关示例,请参阅 http://jsfiddle.net/xaDC4/

As I understand them if you use them standalone they do the same thing. If you try to output the result of them as an expression then they may differ. Try alert(i++) as compared to alert(++i) to see the difference. i++ evaluates to i before the addition and ++i does the addition before evaluating.

See http://jsfiddle.net/xaDC4/ for an example.

回眸一遍 2024-09-20 17:58:05

我有一个关于理解后增量和预增量的解释。所以我把它放在这里。

让我们将 0 分配给 x

let x = 0;

让我们从后递增开始

console.log(x++); // Outputs 0

为什么?

让我们分解 x++ 表达式

x = x;
x = x + 1;

第一个语句返回 x 的值,即 0

稍后当您使用 x 时变量在任意位置,然后执行第二条语句

第二条语句返回此 x + 1 表达式的值,即 (0 + 1) = 1

请记住 < 的值code>x 处于 1 状态

现在让我们从预增量开始

console.log(++x); // Outputs 2

为什么?

让我们分解 ++x 表达式

x = x + 1;
x = x;

第一个语句返回此 x + 1 表达式的值,即 (1 + 1) = 2

第二个语句语句返回 x 的值,即 2 所以 x = 2 因此它返回 2

希望这对您有帮助了解什么是后自增和前自增!

I've an explanation of understanding post-increment and pre-increment. So I'm putting it here.

Lets assign 0 to x

let x = 0;

Lets start with post-increment

console.log(x++); // Outputs 0

Why?

Lets break the x++ expression down

x = x;
x = x + 1;

First statement returns the value of x which is 0

And later when you use x variable anywhere, then the second statement is executed

Second statement returns the value of this x + 1 expression which is (0 + 1) = 1

Keep in mind the value of x at this state which is 1

Now lets start with pre-increment

console.log(++x); // Outputs 2

Why?

Lets break the ++x expression down

x = x + 1;
x = x;

First statement returns the value of this x + 1 expression which is (1 + 1) = 2

Second statement returns the value of x which is 2 so x = 2 thus it returns 2

Hope this would help you understand what post-increment and pre-increment are!

最初的梦 2024-09-20 17:58:05
var a = 1;
var b = ++a;
alert('a:' + a + ';b:' + b); //a:2;b:2

var c = 1;
var d = c++;
alert('c:' + c + ';d:' + d); //c:2;d:1

jsfiddle

var a = 1;
var b = ++a;
alert('a:' + a + ';b:' + b); //a:2;b:2

var c = 1;
var d = c++;
alert('c:' + c + ';d:' + d); //c:2;d:1

jsfiddle

独﹏钓一江月 2024-09-20 17:58:05
var x = 0, y = 0;

//post-increment: i++ returns value then adds one to it
console.log('x++ will log: ', x++); //0
console.log('x after x++ : ', x);    //1

//pre-increment: adds one to the value, then returns it
console.log('++y will log: ', ++y); //1
console.log('y after ++y : ', y);   //1
var x = 0, y = 0;

//post-increment: i++ returns value then adds one to it
console.log('x++ will log: ', x++); //0
console.log('x after x++ : ', x);    //1

//pre-increment: adds one to the value, then returns it
console.log('++y will log: ', ++y); //1
console.log('y after ++y : ', y);   //1
霓裳挽歌倾城醉 2024-09-20 17:58:05

如果可能的话,使用 ++i 会更清晰、更快:

  • ++i 保证您使用的 i 值将保持为除非您更改 i ,否则相同
  • i++ 允许使用 i 的值,该值将在“不久的将来”发生变化,如果可能的话,这是不可取的

当然,速度并没有快多少,只是快了一点点。

It is clearer and faster to use ++i if possible :

  • ++i guarantees that you are using a value of i that will remains the same unless you change i
  • i++ allows to use a value of i which will change in the "near future", it is not desirable if possible

Of course, it's not really much faster, only a little.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文