2014-01-08 7 views
1

누구나 isSharp이 아래 코드의 global context에있는 방법을 알려 줄 수 있습니까? 나는 존의 Advanced JS를 따랐다. 개체에서 실행되지 않는 코드에서컨텍스트는 정확히 무엇을 나타 냅니까?

function katana(){ 
    this.isSharp = true; 
} 
katana(); 
alert(isSharp); 
assert(isSharp === true, "A global object now exists with that name and value."); 

var shuriken = { 
    toss: function(){ 
    this.isSharp = true; 
    } 
}; 
shuriken.toss(); 
assert(shuriken.isSharp === true, "When it's an object property, the value is set within the object."); 
+2

아마도 MDN의 [this 키워드] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this)를 읽어야합니다. – Bergi

답변

3

http://ejohn.org/apps/learn/#24

- this 글로벌 문맥을 말한다.

이것은 this이 언어로 작동하는 방식의 일부입니다.

주 - 엄격 모드를 사용해야하는 경우 TypeError가 발생합니다.


참조 : "entering function code" : 그렇지

thisArg가 null 불확정 인 경우, 글로벌 물체에 ThisBinding 세트.

관련 문제