请解释.call(false)的奇怪行为

时间:2011-02-02 19:24:09

标签: javascript boolean

> (function () { return this; }).call(false)
false

> !!(function () { return this; }).call(false)
true

Firefox 4 beta和Chrome最新版本。

就像......什么时候是布尔值,而不是布尔值?

2 个答案:

答案 0 :(得分:6)

当一个原始布尔值作为第一个参数传递给callapply时,它会被自动装入Boolean个对象。这在Firefox 4上的Firebug中很明显:

>>> (function () { return this; }).call(false)
Boolean {}

在Chrome的检查员中,它最初令人困惑,但有一点探索揭示了真相:

>>> (function () { return this; }).call(false)
false
>>> typeof (function () { return this; }).call(false)
"object"

所有JavaScript对象都是“真实的”,甚至是new Boolean(false)new Number(0)。因此,使用两个否定运算符(!!技巧)将它们转换为true布尔值。

答案 1 :(得分:4)

我在规范中找到了解释行为的这一行。

3. Else if Type(thisArg) is not Object, set the 
   ThisBinding to ToObject(thisArg). 

本质上,false值将转换为布尔对象。 !运算符的第一个应用程序会将对象转换为true,然后转换为false!运算符的第二个应用会将false反转为true

全文

10.4.3  Entering Function Code 

The following steps are performed when control enters the 
execution context for function code contained in function 
object F, a caller provided thisArg, and a caller provided argumentsList: 

1.  If the function code is strict code, set the ThisBinding to  thisArg. 
2.  Else if thisArg is null or undefined, 
    set the ThisBinding to the global object.  
3.  Else if Type(thisArg) is not Object, set the 
    ThisBinding to ToObject(thisArg). 
4.  Else set the ThisBinding to thisArg. 
5.  Let localEnv be the result of calling NewDeclarativeEnvironment   
    passing the value of the [[Scope]] internal 
    property of F as the argument. 
6.  Set the LexicalEnvironment to  localEnv. 
7.  Set the VariableEnvironment to localEnv. 
8.  Let code be the value of F‘s [[Code]] internal property.  
9.  Perform Declaration Binding Instantiation using the function code  
    code and argumentsList as described in 
相关问题