assign multiple variables to the same value in Javascript

I have initialized several variables in the global scope in a JavaScript file:

var moveUp, moveDown, moveLeft, moveRight;
var mouseDown, touchDown;

I need to set all of these variables to false, this is the code I currently have:

moveUp    = false;
moveDown  = false;
moveLeft  = false;
moveRight = false
mouseDown = false;
touchDown = false;

Is there any way that I can set all of these variables to the same value in one line of code, or is the code I currently have the best way to do this


Nothing stops you from doing

moveUp = moveDown = moveLeft = moveRight = mouseDown = touchDown = false;

Check this example

var a, b, c;
a = b = c = 10;
console.log(a + b + c)

Nothing stops you from doing the above, but hold up!

There are some gotchas. Assignment in Javascript is from right to left so when you write:

var moveUp = moveDown = moveLeft = moveRight = mouseDown = touchDown = false;

it effectively translates to:

var moveUp = (moveDown = (moveLeft = (moveRight = (mouseDown = (touchDown = false)))));

which effectively translates to:

var moveUp = (window.moveDown = (window.moveLeft = (window.moveRight = (window.mouseDown = (window.touchDown = false)))));

Inadvertendly, you just created three 5 global variables--something I'm pretty sure you didn't want to do.

Note: The above example assumes you are running your code in the browser, hence window . If you were to be in a different environment these variables would attach to whatever the global context happens to be for that environment (ie, in Node.js, it would attach to process which is the global context for that environment).

Long story short, you should stick to the long way. It's ugly, there's more characters to type, but at least you won't be committing the sin of cluttering the global namespace without any good reason and get weird stares from other people (jk).

Also, as pointed out in the comments (and this is not just in the case of this question), whenever assigning objects, the reference to the object is copied instead of the actual object. Both variables will still point to the same object so any change in one variable will be reflected in the other variable and defeats the purpose of your end-goal.


There is another option that does not introduce global gotchas when trying to initialize multiple variables to the same value. Whether or not it is preferable to the long way is a judgement call. It will likely be slower and may or may not be more readable. In your specific case, I think that the long way is probably more readable and maintainable as well as being faster.

The other way utilizes Destructuring assignment.

let [moveUp, moveDown,
     moveLeft, moveRight,
     mouseDown, touchDown] = Array(6).fill(false);

console.log(JSON.stringify({
    moveUp, moveDown,
    moveLeft, moveRight,
    mouseDown, touchDown
}, null, '  '));

// NOTE: If you want to do this with objects, you would be safer doing this
let [obj1, obj2, obj3] = Array(3).fill(null).map(() => ({}));
console.log(JSON.stringify({
    obj1, obj2, obj3
}, null, '  '));
// So that each array element is a unique object

// Or another cool trick would be to use an infinite generator
let [a, b, c, d] = (function*() { while (true) yield {x: 0, y: 0} })();
console.log(JSON.stringify({
    a, b, c, d
}, null, '  '));

// Or generic fixed generator function
function* nTimes(n, f) {
    for(let i = 0; i < n; i++) {
        yield f();
    }
}
let [p1, p2, p3] = [...nTimes(3, () => ({ x: 0, y: 0 }))];
console.log(JSON.stringify({
    p1, p2, p3
}, null, '  '));
链接地址: http://www.djcxy.com/p/69978.html

上一篇: Javascript a = b = c语句

下一篇: 在Javascript中将多个变量分配给相同的值