Enum flags in JavaScript

前端 未结 6 443
夕颜
夕颜 2021-01-30 08:23

I need to emulate enum type in Javascript and approach seems pretty straight forward:

var MyEnum = {Left = 1; Right = 2; Top = 4; Bottom = 8}

N

6条回答
  •  隐瞒了意图╮
    2021-01-30 08:58

    Yes, bitwise arithmetic works in Javascript. You have to be careful with it because Javascript only has the Number data type, which is implemented as a floating-point type. But, values are converted to signed 32-bit values for bitwise operations. So as long as you don't try to use more than 31 bits, you'll be fine.

提交回复
热议问题