블록 스코프 : 감싸주면 this 를 밖에 있는 this를 그대로 씀 this바인딩을 하지 않음 함수 스코프 : 전역을 바라보게 되어 전역을 this로 인식함
var value = 0;
var obj = {
value: 1,
setValue: function (){
let a = 20;
this.value = 2;
{
let a = 10;
this.value = 3;
}
}
}
======================================================
여기서 보는 것과 같이 this.value는 this는 같은 곳을 보게됨.
var value = 0;
function () {
this.value = 3; //
}
======================================================
this.value 는 전역을 바라보게됨.
var value = 0;
var obj = {
value: 1,
setValue: function (){
this.value = 2; // this: obj -> obj.value = 2;
//메소드를 실행한 경우 메소드. 앞에가 this가 됨
(function () {
this.value = 3; // this : window -> 그냥 함수를 실행할 경우 전역객체를 보게됨
})();
}
}
obj.setValue(); //메소드를 실행
console.log(value);
console.log(obj.value);
====================================================================
3
2