2011-02-02 3 views

답변

6

. 이 파이어 폭스 4에 방화범에 분명하다

>>> (function() { return this; }).call(false) 
Boolean {} 
크롬의 관리자의

, 그것은 처음에는 혼란하지만 약간의 프로빙 진실 밝혀 : 심지어 new Boolean(false)

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

모든 자바 스크립트 객체는을 "truthy"입니다을 및 new Number(0). 따라서 두 개의 부정 연산자 (!! 트릭)를 사용하여이를 true 부울로 변환합니다.

4

이 줄은 동작을 설명하는 사양에서 발견되었습니다.

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

기본적으로 false 값은 부울 개체로 변환됩니다. ! 연산자의 첫 번째 적용은 개체를 true으로 변환 한 다음 false으로 반전합니다. ! 연산자의 두 번째 적용은 falsetrue으로 반전합니다.

전체 텍스트 원시적 부울이 call 또는 apply에 첫 번째 인수로 전달 될 때, 그것은 자동 박스 Boolean 객체로 않은 것 같습니다

 
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 스펙을 언급 한 이후로 받아 들여진 대답으로 갈 수밖에 없다는 것을 유감스럽게 생각합니다.하지만 ide의 대답은 꽤 따라하기 쉽기 때문에이 문제를 해결할 수있는 다른 모든 사람들에게 꼭 보여주고 싶습니다. :) – Domenic

+0

@Domenic - 그들이 조금 더 깊이 파고 싶다면 조금 아래로 스크롤하는 것이 이해할 만합니다. – ChaosPandion

관련 문제