Функция Boolean
возвращает true
для всех ссылок на объекты.new String("")
создает строку объект .Напротив, ""
является просто строковым примитивом;Boolean
возвращает false
для пустого строкового примитива.
При вызове как функции (а не как конструктора), Boolean
возвращает результат спецификации ToBoolean
абстрактная операция :
Абстрактная операция ToBoolean преобразует аргумент в значение типа Boolean в соответствии с таблицей 9:
+−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−+
| 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
.