最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

x+=y+=z in Javascript - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited May 14, 2012 at 13:25 Eric Yin asked May 14, 2012 at 12:40 Eric YinEric Yin 8,97321 gold badges80 silver badges122 bronze badges 11
  • 29 Sometimes it's better to not trying to compress everything into a single line of code but write a few symbols more and keep readability... – Christoph Commented May 14, 2012 at 12:43
  • 13 Holy obfuscation, Batman! Just use x += z and y += z. – JJJ Commented May 14, 2012 at 12:43
  • 7 You're making two assignments. Why not be explicit about it? What's the need for short code? – Michael Berkowski Commented May 14, 2012 at 12:43
  • 7 Is it for a javascript-in-a-twitter competition? :) – biziclop Commented May 14, 2012 at 12:45
  • 4 @ninjagecko. Sony playstation. :) – gdoron Commented May 14, 2012 at 12:52
 |  Show 6 more comments

9 Answers 9

Reset to default 21

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);
发布评论

评论列表(0)

  1. 暂无评论