<aside> 💡 this는 함수 호출하는 방식, 즉 함수가 어떻게 호출되었는지에 따라 결정된다
</aside>
this는 myObject
를 가리킨다 ⇒ 메소드를 호출한 객체가 바인딩된다.
const myObject = {
value: 10,
show() {
console.log(this.value);
}
}
myObject.show(); // 10
일반함수로 호출하면 함수 내부의 this에는 전역 객체(window)
가 바인딩된다.
function show() {
this.value = 1;
console.log(this);
}
show(); // window
const myObject = {
value: 1,
show1() {
console.log(this.value); // 1
function show2() {
console.log(this.value); // ??
}
show2();
}
};
myObject.show1();