X + = y + = z in Javascript

I know in javascript

x = y = z means x = z and y = z

x+=z means x=x+z ;

So, if I want x=x+z and y=y+z , I tried x+=y+=z not work

does anyone have an idea to write a short code instead of x+=z;y+=z

EDIT

First thanks to everyone who is involved in my question. Here I want to explain why I have this question in the first place.

I tried to write code like x+='some html code' and I need y+='the same html code' . Therefore, naturally, I do not want to first create another var z='the html code , and then x+=z and y+=z

Hope my clarify the meaning. Anyway, I'm going to close this question now. Thanks again.

+6
source share
9 answers

Assuming padding and not concatenation , this works:

 x -= y - (y += z); 

but seriously, don't use it !


For those who want to find out how the evaluation sequence (where I use n to display the current intermediate result) is approximately equal to:

 n = y1 = y0 + z // n = y = (y + z) n = y0 - y1 // -> n == -z [uses the original value of y] x -= n // -> x += z 
+20
source

Just use this:

 x+=z;y+=z 

Honestly, everything else will just force someone else to support your code so that it stops and scratches its head for a couple of minutes. This code is not shocking for a long time ...

+10
source

Well, x + = y + = z means:

  • x = x + y + z
  • y = y + z

Thus, it is impossible to do x = x + z and y = y + z with x + = y + = z, because this means:

 x += (y = y + z) -> x = x + (y = y + z) 

Take the following example:

 function sum () { var x = 5, y = 7, z = 3; x += y += z; console.log (x); // it shows 15 --> x = 5 + 7 + 3 console.log (y); // it shows 10 --> y = 7 + 3 console.log (z); // it shows 3 } 

So you should do it like this:

 x += z; y += z; 
+6
source

In fact, x = y = z does not mean x = z and y = z . This means calculating the value of the expression y = z , and then assign x the value of this expression. You are wrong here.

+3
source

you can also do .. x = 1, y = 2, z = 3

 x+=(y+=z)-(yz) 
+3
source

You can use the comma operator:

 x += (y+=z,z); 
+3
source

It does not work, because what is done in the assignment

 x += y += z; 

is an:

 y += z 

ranked first. Besides adding z to x , this assignment also returns a new y value as the return value. Then this new y value becomes the operand for the other += .

 x += y 

Well, there is probably no shorter way to write what you want than just

 x += z; y += z; 
+2
source

FWIW, in Firefox, you can use the destructuring assignment to do something like what you want.

 Array.prototype.addToEach = function(x) { for (var i = 0; i < this.length; i++) this[i] += x; return this; }; var x = "foo", y = "bar"; [x,y] = [x,y].addToEach("baz"); console.log(x,y); // "foobaz" "barbaz" 

http://jsfiddle.net/uPzNx/ (demo for spidermonkey implementation)


Not that this has anything to do with the solution, but for those who don't like the native .prototype extensions, you can do it instead.

 function addToEach(s) { var args = Array.prototype.slice.call(arguments, 1); for (var i = 0; i < args.length; i++) args[i] += s; return args; }; var x = "foo", y = "bar"; [x,y] = addToEach("baz", x, y); console.log(x,y); // "foobaz" "barbaz" 
+1
source

This works on jsfiddle, so it should be something else that you do ...

 x = 1, y = 2, z = 3; x += y += z; alert(x); 
-2
source

Source: https://habr.com/ru/post/915674/


All Articles