这在 javascript 中可行吗?
let a, b; // Both objects, eg vectors
let bl = false; // This is a bool that could be true or false
// Is this possible, or how could it be done
(bl ? a : b) = {x: 5, y: -2, z: 3};
即,我想根据 bl
有条件地将 a
或 b
设置为该向量。
或者是唯一的方法:
let tmp = {x: 5, y: -2, z: 3};
if(bl) a = tmp;
else b = tmp;
我只是觉得应该有一种更优雅的方式来做到这一点。
最佳答案
在某种程度上是这样,但它只是您作为替代方案所写内容的简写。
let a, b;
let bl = true;
let obj = {x: 5, y: -2, z: 3}
bl ? a = obj : b = obj;
https://stackoverflow.com/questions/74214767/