Can an Object be false?

前端 未结 9 1582
温柔的废话
温柔的废话 2020-12-06 04:35

Is there any way to make an object return false in javascript?

var obj = new Object();

console.log(!!obj) // prints \"true\" even if it\'s empty


        
相关标签:
9条回答
  • 2020-12-06 04:49

    I think that with the first ! you are casting obj to a boolean and negating its value- resulting in true if obj is null - , and with the second ! negating it again.

    0 讨论(0)
  • 2020-12-06 04:52

    No. But null will convert to false.

    > typeof(null)
    "object"
    > null instanceof Object
    false
    > !!null
    false
    

    To see if the object contains any properties, use (shamelessly copied from How do I count a JavaScript object's attributes?):

    function isEmpty (obj) {
        for (var k in obj) 
           if (obj.hasOwnProperty(k))
               return false;
        return true;
    }
    
    0 讨论(0)
  • 2020-12-06 04:53

    As of ES8, no, you cannot make an object evaluates to false in JavaScript.

    In the specification, all boolean checks (? ! if etc.) depends on ToBoolean,
    which is very, very simple:

    • False if undefined, null, false, zero, NaN, or empty string.
    • True for everything else (Object, Proxy, Symbol, etc.)

    If the type of the input is object, the result is true. No question asked. No valueOf, no special case.

    There is no way to create a falsy object in JavaScript. Only non-objects can be falsy.

    Sometimes you may run into object-like "stuff" that return false. Empty string, for example, are used like an object all the time. document.all is another falsy "object".

    These are not real objects, however. They cannot have custom properties, cannot be used as prototype, and does not always behave like an object e.g. typeof or strict equal.

    This behaviour is most likely here to stay for backward compatibility.

    0 讨论(0)
  • 2020-12-06 04:53

    No.

    Not sure why you'd want this, but there is a way you could do something like this but it's kinda hacky...

    var obj = {
        toString: function() { return ''; }
    };
    
    alert(!! (''+obj));
    
    0 讨论(0)
  • 2020-12-06 05:00

    We can overwrite Object.prototype.valueOf to make an object appear to be false when it is coerced into a primitive, for example during ==.

    However it will not appear to be false when we force it into a boolean using !!, so it doesn't really work in the general case.

    var obj = {
        valueOf: function () {
            return false
        }
    }
    
    > obj == false
    true               // Good, we fooled them!
    
    > !!obj
    true               // Not so good, we wanted false here
    
    > Boolean(obj)
    true               // Not so good, we wanted false here
    
    0 讨论(0)
  • 2020-12-06 05:07

    No. An object that doesn't have any properties assigned is not considered "empty".

    The fact that a variable holds an instance of an object is enough to cause javascript to treat the variable as having the value of true when an expression requires a boolean value.

    Edit

    There are clearly some nuances to be cleared up looking at the other answers here.

    null is not an object, it is the distinct lack of an object. The question refers to an Object, that is one that has just been created.

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