Can an Object be false?

前端 未结 9 1587
温柔的废话
温柔的废话 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: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));
    

提交回复
热议问题