assign multiple variables to the same value in Javascript

前端 未结 5 2010
Happy的楠姐
Happy的楠姐 2020-11-30 18:40

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

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


        
5条回答
  •  北海茫月
    2020-11-30 19:14

    The original variables you listed can be declared and assigned to the same value in a short line of code using destructuring assignment. The keywords let, const, and var can all be used for this type of assignment.

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

提交回复
热议问题