I have initialized several variables in the global scope in a JavaScript file:
我在JavaScript文件中初始化了全局范围内的几个变量:
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:
我需要将所有这些变量设置为false,这是我目前拥有的代码:
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
有没有办法可以将所有这些变量设置为一行代码中的相同值,或者我现在有最好的方法来执行此操作的代码
3 个解决方案
#1
187
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)
#2
27
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:
有一些陷阱。 Javascript中的赋值是从右到左所以当你写:
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.
不经意的是,你刚刚创建了三个5个全局变量 - 我很确定你不想这样做。
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 (i.e., in Node.js, it would attach to process
which is the global context for that environment).
注意:上面的示例假设您在浏览器中运行代码,因此窗口。如果您处于不同的环境中,这些变量将附加到该环境的全局上下文(即,在Node.js中,它将附加到作为该环境的全局上下文的进程)。
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).
长话短说,你应该坚持漫长的道路。这很难看,还有更多的字符可以输入,但至少你不会在没有任何充分理由的情况下犯下混乱全局命名空间的罪恶,并从其他人(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.
此外,正如评论中指出的那样(这不仅仅是在这个问题的情况下),每当分配对象时,都会复制对象的引用而不是实际的对象。两个变量仍将指向同一个对象,因此一个变量中的任何更改都将反映在另一个变量中,并且会破坏最终目标的目的。
#3
2
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, ' '));
This allows you to initialize a set of var
, let
, or const
variables to the same value on a single line all with the same expected scope.
这允许您将一组var,let或const变量初始化为一行中具有相同预期范围的相同值。
References:
参考文献:
-
MDN: Array Global Object
MDN:数组全局对象
-
MDN: Array.fill
MDN:Array.fill
#1
187
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)
#2
27
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:
有一些陷阱。 Javascript中的赋值是从右到左所以当你写:
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.
不经意的是,你刚刚创建了三个5个全局变量 - 我很确定你不想这样做。
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 (i.e., in Node.js, it would attach to process
which is the global context for that environment).
注意:上面的示例假设您在浏览器中运行代码,因此窗口。如果您处于不同的环境中,这些变量将附加到该环境的全局上下文(即,在Node.js中,它将附加到作为该环境的全局上下文的进程)。
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).
长话短说,你应该坚持漫长的道路。这很难看,还有更多的字符可以输入,但至少你不会在没有任何充分理由的情况下犯下混乱全局命名空间的罪恶,并从其他人(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.
此外,正如评论中指出的那样(这不仅仅是在这个问题的情况下),每当分配对象时,都会复制对象的引用而不是实际的对象。两个变量仍将指向同一个对象,因此一个变量中的任何更改都将反映在另一个变量中,并且会破坏最终目标的目的。
#3
2
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, ' '));
This allows you to initialize a set of var
, let
, or const
variables to the same value on a single line all with the same expected scope.
这允许您将一组var,let或const变量初始化为一行中具有相同预期范围的相同值。
References:
参考文献:
-
MDN: Array Global Object
MDN:数组全局对象
-
MDN: Array.fill
MDN:Array.fill