是否确保Java中的操作执行顺序始终相同? [英] Is the order of execution of operations in Javascript guaranteed to be the same at all times?

查看:49
本文介绍了是否确保Java中的操作执行顺序始终相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我做这样的事情时:

var x = 5;
console.log(         x  + (x += 10)  );  //(B) LOGS 10, X == 20
console.log(  (x += 10) +  x         );  //(A) LOGS  0, X == 30

(A)与(B)之间返回值的差异由 x 的值解释时来解释.我认为应该在后台进行这样的事情:

The difference in the returned value between (A) and (B) is explained by the value of x at the time it becomes evaluated. I figure that backstage something like this should happen:

     TIME ---->
(A)         (x = 5) + (x += 10 = 15) = 20
(B) (x += 10 == 15) + (x == 15)      = 30

但这仅在且仅当 x 以与编写时相同的从左至右顺序进行求值时才成立.

But this only holds true if and only if x is evaluated in the same left-to-right order that it was written.

所以,我对此有一些疑问,

So, I have a few questions about this,

保证对所有Javascript实现都是正确的吗?

Is this guaranteed to be true for all Javascript implementations?

标准是否定义为这种方式?

Is it defined to be this way by the standard?

或者,这是Javascript世界中某种未定义的行为吗?

Or, is this some kind of undefined behavior in Javascript world?

最后,相同的想法可以应用于函数调用,

Finally, the same idea could be applied to function calls,

var x = 5;
console.log(x += 5, x += 5, x += 5, x += 5); // LOGS 10, 15, 20, 25

它们似乎也按顺序被评估 ,但是是否有更强的保证力确保这种情况总是发生?

They also appear to be evaluated in order, but is there a stronger guarantee that this should always happen?

推荐答案

对于所有Javascript实现,这是否保证是正确的?

Is this guaranteed to be true for all Javascript implementations?

是的

标准是否定义为这种方式?

Is it defined to be this way by the standard?

是的,您可以在此处阅读.

具体来说,运行时语义:加法运算符( + )的求值指定在左边的表达式之前对左边的表达式求值.

Specifically, the runtime semantics: evaluation of the addition operator (+) specify that the left expression is evaluated before the right expression.

参数列表的评估.

或者,这是Javascript世界中某种未定义的行为吗?

Or, is this some kind of undefined behavior in Javascript world?

是的, JavaScript中没有未定义的行为,但这里没有.

这篇关于是否确保Java中的操作执行顺序始终相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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