2011-02-02 15 views

cevap

6

. Bu Firefox 4 Firebug net olduğundan

>>> (function() { return this; }).call(false) 
Boolean {} 
Chrome'un denetçisinde

, başlangıçta kafa karıştırıcı ama biraz sondalama gerçeği ortaya koymaktadır: Eşit, new Boolean(false)

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

Tüm JavaScript nesneleri "truthy" dir ve new Number(0). Bu nedenle, iki negatif işlecini (!! hile) kullanarak bunları true boolean'a dönüştürür.

4

Bu satırı, davranışı açıklayan belirtimde buldum.

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

Esasen false değer bir Boolean nesnesine dönüştürülecektir. ! operatörünün ilk uygulaması, nesneyi true'a dönüştürecek ve ardından false'a dönecektir. ! operatörünün ikinci uygulaması, false'u true'a çevirecektir.

Tam Metin, ilkel bir Boole call veya apply ilk argüman olarak geçirilir, bu otomatik kutu bir Boolean nesnesine olduğu görülmektedir

 
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 
+0

Oooh Ben, kabul ettiğim cevap olarak bu makaleye başvurduğum için kabulleniyorum… ama ide'nin cevabı takip etmek oldukça kolaydı, bu yüzden bu konuya giren herkes için en üstte tutmak istiyorum :). – Domenic

+0

@Domenic - Anlaşılabilir, biraz daha derin kazmak istiyorlarsa yapmaları gereken tek şey biraz aşağı inmek. – ChaosPandion

İlgili konular