Force an object to evaluate to false

后端 未结 1 1997
小蘑菇
小蘑菇 2021-01-19 04:32

I\'m guessing this is probably application specific however I am using node.js - as long as it works in the v8 engine, I don\'t mind.

I\'m trying to create an

相关标签:
1条回答
  • 2021-01-19 05:20

    The closest you can get is like

    if (+obj)
    

    Where a falsy object defines a .valueOf() function that returns 0 or false:

    var o = {
        valueOf: function() {
            return false;
        }
    };
    if (+o) {
        throw unreachable();
    }
    
    0 讨论(0)
提交回复
热议问题