Enum flags in JavaScript
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.
You just have to use the bitwise operators:
var myEnum = {
left: 1,
right: 2,
top: 4,
bottom: 8
}
var myConfig = myEnum.left | myEnum.right;
if (myConfig & myEnum.right) {
// right flag is set
}
More info:
- Understanding bitwise operations in javascript
- How to check my byte flag?
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) {...}