Shorthand assignment operator for inverting boolean

岁酱吖の 提交于 2019-12-02 08:15:33

Personally I would just negate the thing the way you've done it. But if it really matters to you, and you can afford to forgo the boolean type, just use the numeric internal representation (true==1, false==0) and bitwise operators to negate it:

z = true;  // z is type boolean
z ^= 1;    // z is now false, but its type is number
           // (z == false) evaluates to true and you can still do things like if (z)...
z ^= 1;    // z is now true again
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!