JavaScript In- / Decrement背后的逻辑是什么?有人可以澄清这个吗?

时间:2015-05-12 21:41:19

标签: javascript

++增量

x = ++y     // When y = 6   x = 6   
x = y++     // And y = 6    x = 5   

- 减少

x = --y     // When y = 4   x = 4   
x = y--     // When y = 4   x = 5

有人可以澄清这个吗? 谢谢。

4 个答案:

答案 0 :(得分:2)

唯一有点棘手的案例是:

x = --y     // When y = 4   x = 4

您之后必须查看y 。如果您在之前查看y ,则会看到5

前缀减量(--y)和后缀减量(y--)的基本规则是:

  • 无论哪种方式,操作数(在这种情况下为y)递减1.唯一的区别是该表达式(--yy--)的结果是

  • 如果操作员之前操作数(--y),则为“前缀”递减:递减发生之前表达式的结果被采取。因此,结果是y递减后的值。

  • 如果操作员在操作数(y--)后 ,则为“后缀”减量:在表达式结果后发生被采取。所以结果就是它递减之前的值。

示例:

var x, y;

y = 5;
snippet.log("Before prefix decrement: y = " + y);
x = --y; // Result is the value **after** the decrement
snippet.log("After prefix decrement: x = " + x + ", y = " + y);

y = 5;
snippet.log("Before postfix decrement: y = " + y);
x = y--; // Result is the value **before** the decrement
snippet.log("After postfix decrement: x = " + x + ", y = " + y);
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

答案 1 :(得分:2)

在这两种情况下,WIFEXITED()将具有相同的最终值。唯一的区别在于表达式本身的返回值。

  • WIFSIGNALED()返回 后递增>值。
  • y返回 增量之前的值

答案 2 :(得分:1)

当运算符在变量之前时,它会在任何处理之前更改其值。当变量在运算符之前时,值将在处理后更改。

y = ++x与:

相同
x = x + 1;
y = x;

y = x++与:

相同
y = x;
x = x + 1;

答案 3 :(得分:1)

Since your main question is:

The logic behind ...

The answer to that part specifically are processors. Many have had the app.controller('mainDashboardApi', ['$scope', 'dashboard', function($scope, dashboard) { $scope.dashboard = dashboard; } ]); and INC instructions for a very long time and since C was created to match the processor instructions, it was thought that having operators such as DEC and ++ would help in optimizing some code.

--

RISC processors generally require you to use INC WORD [BP+4] ; increment value at address BP+4 by one DEC WORD [SP+16] ; decrement value at address SP+16 by one and ADD instead. The result is pretty much the same in modern processor because speed of execution of such instructions was greatly enhanced, especially with large instruction caches.

Java and JavaScript are spawned of C and thus inherited these.

Specifically, there is the 68000 processor that has instructions such as:

SUB

which include hidden increment (the +) and hidden decrement (the -) in the instruction.

In these cases, C matched the processor behavior perfectly. You could write something like:

MOV.W D1, (A1)+    ; copy D1 at address A1, then increment A1 by size of D1

MOV.W D2, -(SP)    ; decrement stack by size of D2, then copy D2 at address SP

And each line becomes just one instruction in assembly language. Notice that the *a1++ = d1; *--sp = d2; and ++ used with the stack work like a charm:

--

Here we want to use register d1, so we save it on the stack, the automatic pre-decrement works exactly as required. Then before leaving the function, we restore from the stack, again, the automatic post-increment works exactly as expected. The INTEL processors have specialized instructions instead: *--sp = d1; // move the stack pointer, then save value on the stack ... // do some work d1 = *sp++; // restore from the stack, then move the stack pointer back and PUSH. But that's the same concept.

Of course, with C++ and other object oriented languages (including Java and JavScript), that has evolved quite a bit now and it is easy to lose the reason why we have had these POP and ++ operators.