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 working

anyone have a better idea to write short code instead x+=z;y+=z

EDIT

First thanks for everyone involved in my question. Here I wanna explain something why I have this question in first place.

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

Hope my explain make sense. Anyway, I am going to close this question now. Thanks again.

Answers


Assuming addition, and not concatenation, this works:

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

but seriously, don't use it !


For those that want to figure out how, the sequence of evaluation (where I use n to show the current intermediate result) is approximately:

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

Just use this:

x+=z;y+=z

Honestly, anything else is just going to cause somebody else maintaining your code to stop and scratch their head for a couple of minutes. This code isn't shockingly long either...


Well, x += y += z means:

  1. x = x + y + z
  2. y = y + z

So, it's impossible to do x = x + z and y = y + z with x += y += z because it would 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 have to do it as follows:

x += z;
y += z;

Actually x = y = z doesn't mean x = z and y = z. It means calculate a value of expression y = z and then assign x the value of that expression. This is where you wrong.


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

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

You can use comma operator:

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

It is not working, because what is done in the assignment

x += y += z;

is:

y += z

is evaluated first. Besides adding z to x, this assignment also returns the new value of y as its return value. Then, this new value of y becomes the operand to the other +=.

x += y

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

x += z; y += z;

FWIW, in Firefox you can use 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 implementations)


Not that it has much to do with the solution, but for those who dislike native .prototype extensions, you could do this 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"

This works on jsfiddle, so it must be something else you are doing...

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

Need Your Help

Using Objective-C Dynamic properties with generic setter and ARC

ios objective-c dynamic automatic-ref-counting

I would like to use dynamic properties with generic getter/setter, but it seems that in some cases ARC releases an object when it should not. I give here the simplest code to show the problem.

Visual Studio - Tool to replace hard coded strings with resources

c# asp.net resources string

I have a big ASP.NET project full of hard coded strings, that now have to be extracted to resources. Such strings can be found almost anywhere (in .cs files, .aspx files, .ascx files, etc.) and the