Enum flags in JavaScript

前端 未结 6 450
夕颜
夕颜 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 09:01

    In javascript you should be able to combine them as:

    var left_right = MyEnum.Left | MyEnum.Right;
    

    Then testing would be exactly as it is in your example of

    if ( (left_right & MyEnum.Left) == MyEnum.Left) {...}
    

提交回复
热议问题