What's the point of the Boolean object?

前端 未结 9 1048
遥遥无期
遥遥无期 2020-12-09 15:28

I am reading through the Mozilla Manual on JavaScript, and I come to this point in my reading, Boolean object. I can\'t see a single use for them. What\'s their point? Why w

相关标签:
9条回答
  • 2020-12-09 15:53
    Boolean.prototype.bang = function() {
        return !this.valueOf();
    }
    
    true.bang(); // false
    

    Everything in JavaScript is an object. But at the same time we also have primitives. It's really confusing, just don't overthink it.

    0 讨论(0)
  • 2020-12-09 15:58

    Perhaps because JavaScript objects are extensible in ways that primitives aren't? (I'm just guessing here, I've never had a need for Boolean.

    0 讨论(0)
  • 2020-12-09 16:08

    JavaScript language design has quite many dusty corners, and the Boolean is one of them; it is not used in practice.

    This:

    var a = [];
    alert(a instanceof Array);
    

    will tell you "true". But this:

    var b = true;
    alert(b instanceof Boolean);
    

    for some reason will show "false".

    In short: forget about it.

    0 讨论(0)
提交回复
热议问题