相关疑难解决方法(0)

使用按位OR 0来设置数字

我的一位同事偶然发现了一种方法来使用按位或者:

var a = 13.6 | 0; //a == 13
Run Code Online (Sandbox Code Playgroud)

我们在谈论它并想知道一些事情.

  • 它是如何工作的?我们的理论是使用这样的运算符将数字转换为整数,从而删除小数部分
  • 它有什么优势Math.floor吗?也许它快一点?(双关语不打算)
  • 它有任何缺点吗?也许它在某些情况下不起作用?清晰度是显而易见的,因为我们必须弄清楚,而且,我正在写这个问题.

谢谢.

javascript floating-point bit-manipulation

178
推荐指数
6
解决办法
4万
查看次数

如何在多个 React Redux 组件中使用 requestAnimationFrame 实现游戏循环?

努力想出最好的方法来解决它。我可以使用递归调用requestAnimationFrame来进行游戏循环:

export interface Props {
    name: string;
    points: number;
    onIncrement?: () => void;
    onDecrement?: () => void;
}

class Hello extends React.Component<Props, object> {

    constructor(props: Props) {
        super(props);
    }

    render() {
        const { name, points, onIncrement, onDecrement } = this.props;

        return (
            <div className="hello">
                <div className="greeting">
                    Hello {name + points}
                </div>
                <button onClick={onDecrement}>-</button>
                <button onClick={onIncrement}>+</button>
            </div>
        );
    }

    componentDidMount() {
        this.tick();
    }

    tick = () => {
        this.props.onIncrement();
        requestAnimationFrame(this.tick)
    }

}
Run Code Online (Sandbox Code Playgroud)

但是如果我想要在每一帧上怎么办:

  • Component1 做 X
  • Component2 做 Y …

javascript typescript reactjs redux react-redux

6
推荐指数
1
解决办法
3591
查看次数