当我测试这个时,我只是在玩弄 nodejs 和 chrome 的控制台:
[] == true // false
![] == true // false
!![] == true // true
怎么会?是不是错了?
最佳答案
参见 ECMAScript standard :
11.4.9 Logical NOT Operator ( ! )
The production UnaryExpression : ! UnaryExpression is evaluated as follows:
- Let expr be the result of evaluating UnaryExpression.
- Let oldValue be ToBoolean(GetValue(expr)).
- If oldValue is true, return false.
- Return true.
9.2 ToBoolean
The abstract operation ToBoolean converts its argument to a value of type Boolean according to Table 11:
- undefined → false
- null → false
- Boolean → The result equals the input argument (no conversion).
- Number → The result is false if the argument is +0, -0, or NaN; otherwise the result is true.
- The result is false if the argument is the empty String (its length is zero); otherwise the result is true.
- Object → true
数组是一个对象。
关于javascript - 为什么 ![] 不是真的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11025870/