对象可以是假的吗? [英] Can an Object be false?

查看:195
本文介绍了对象可以是假的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让对象在javascript中返回false?

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


推荐答案

不。没有分配任何属性的对象不被视为空。

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

变量保存对象实例的事实足以导致javascript处理当表达式需要布尔值时,变量的值为 true

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.

编辑

在这里查看其他答案时,显然有一些细微差别需要澄清。

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

null 不是一个对象,它是一个明显缺乏的对象。该问题涉及一个Object,即刚刚创建的Object。

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.

这篇关于对象可以是假的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆