我想检查变量是否为假、真或空。如果为 null 或未定义,则默认将数组分配给变量。在其他语言中,这种语法很好用。然而,在 js 中,当值为 false 时,它将数组分配给myBool
const boolFromBody = false;
const myBool = boolFromBody || [true, false];
console.log(myBool)Run Code Online (Sandbox Code Playgroud)
我设法用这种语法来做到这一点,我只检查空值
const boolFromBody = null;
let otherBool = boolFromBody;
if (boolFromBody === null) {
otherBool = [true, false]
}
console.log(otherBool);Run Code Online (Sandbox Code Playgroud)
有没有更好的方法在js中做到这一点?
由于元素可以根据其内容改变其宽度,因此我不知道如何获取其宽度(以像素为单位)。
这是基本的简化结构:
// React component
export const Navbar = ({ month }) => (
<NavbarStyled>
<div>
<span>{month}</span> //month is what varies in width
</div>
</NavbarStyled>
)
Run Code Online (Sandbox Code Playgroud)
// NavbarStyled.js
export const NavbarStyled = styled.nav`
...
span:after{
...
animation: show 1s ease forwards;
}
@keyframes show{
100%{
transform: translateX(the_element's_width_in_px);
}
}
`
Run Code Online (Sandbox Code Playgroud)
我尝试了很多事情但没有结果。
谢谢!