我怀疑两者都是字符串,为什么获取不同的布尔值 Boolean('')为false和 Boolean(new String('')) ?
答案 0 :(得分:3)
Boolean
函数为所有对象引用返回true
。 new String("")
创建一个字符串 object 。相反,""
只是一个字符串基元。 Boolean
返回false
以获得空白字符串基元。
当作为函数(而不是构造函数)调用时,Boolean
返回规范的ToBoolean
abstract operation的结果:
抽象操作ToBoolean根据表9将参数转换为Boolean类型的值:+−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−+ | Table 9: ToBoolean Conversions | +−−−−−−−−−−−−−−−+−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−+ | Argument Type | Result | +−−−−−−−−−−−−−−−+−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−+ | Undefined | Return false. | +−−−−−−−−−−−−−−−+−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−+ | Null | Return false. | +−−−−−−−−−−−−−−−+−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−+ | Boolean | Return argument. | +−−−−−−−−−−−−−−−+−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−+ | Number | If argument is +0, −0, or NaN, return false; | | | otherwise return true. | +−−−−−−−−−−−−−−−+−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−+ | String | If argument is the empty String (its length is | | | zero), return false; otherwise return true. | +−−−−−−−−−−−−−−−+−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−+ | Symbol | Return true. | +−−−−−−−−−−−−−−−+−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−+ | Object | Return true. | +−−−−−−−−−−−−−−−+−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−+
从表的最后一行可以看到,任何对象对象都将产生true
。
答案 1 :(得分:0)
false
,因为它们是falsy
值:''
,NaN
,undefined
,null
,{{1 }}。0
,因为它们是true
值。您要使用的功能:
truthy
并包含以下内容:
Boolean('') // Coercing a primitive empty string (falsy).