Dom*_*nic 6 javascript boolean
> (function () { return this; }).call(false)
false
> !!(function () { return this; }).call(false)
true
Run Code Online (Sandbox Code Playgroud)
在Firefox 4 beta和Chrome中都是最新的.
它就像......什么时候是布尔值,而不是布尔值?
看起来当一个原始布尔值作为第一个参数传递给call或时apply,它会被自动装入一个Boolean对象.Firefox 4上的Firebug中很明显:
>>> (function () { return this; }).call(false)
Boolean {}
Run Code Online (Sandbox Code Playgroud)
在Chrome的检查员中,它最初令人困惑,但有一点探索揭示了真相:
>>> (function () { return this; }).call(false)
false
>>> typeof (function () { return this; }).call(false)
"object"
Run Code Online (Sandbox Code Playgroud)
所有JavaScript对象都是"真实的",甚至new Boolean(false)和new Number(0).因此,使用两个否定运算符(!!技巧)将它们转换为true布尔值.