You can initialize two variables to the same value at once, kinda:
var foo, bar; foo = bar = 10;
But there is no similarly easy mechanism to add, say, 5 to both
bar at the same time. Of course, it’s simple enough to just do:
foo += 5; // foo is now 15 bar += 5; // bar is now 15
But that’s two operations.
The trick is:
foo += -bar + (bar += 5); // foo and bar are now 15
You will probably never need this, it’s just interesting to know it’s possible.
Thanks to Matheus Avellar for sending in this little mindbending trick, who also explains:
-bargets parsed and becomes the negative value of
bar, so -10. Then,
a += 5runs and sets
barto 15 (10 + 5). Finally, it sums up both values (-10 + 15) and gives you the difference between old
bar, which is 15.
- You must login to post comments
Please login first to submit.