참고자료 : ZeroCho [인간 JS 엔진 되기 1-6]this는 호출 때 결정된다고!!!
this는 기본적으로 window이다.
this는 함수가 호출되기 전까지 아무도 모른다.
const obj = {
name: 'ryan',
sayName() {
console.log(this.name);
},
};
obj.sayName(); // this > obj
const sayN = obj.sayName;
sayN(); // this > window
const obj2 = {
name: 'ryan',
sayName: () => {
console.log(this.name);
},
};
obj.sayName(); // 화살표 함수의 this는 window를 가리킴
function Human(name) {
this.name = name;
}
new Human('ryan'); // name : 'ryan'
// new를 통해 함수를 호출하는 경우 this는 객체를 가리킴
function sayName() {
console.log(this.name);
}
sayName.bind({name: 'ryan'})(); // ryan
sayName.apply({name: 'ryan'}); // ryan
sayName.call({name: 'ryan'}); // ryan
// bind call apply로 this를 직접적으로 바꿀 수 있음
const obj3 = {
name: 'ryan',
sayName: () => {
console.log(this.name); // this > obj
function inner() {
console.log(this.name); // this > window
}
inner(); // this를 바꿔주는 동작이 없음 // inner의 스코프 체인은 sayName
},
};
// obj3.sayName() // this > window
const obj4 = {
name: 'ryan',
sayName: () => {
console.log(this.name); // this > obj
const inner = () => {
console.log(this.name); // this > obj
};
inner(); // 화살표 함수는 부모 함수가 '호출'될 때의 this를 그대로 가져옴
},
};
obj4.sayName(); // ryan, ryan
const header = document.querySelector('#header');
header.addEventListener('click', function () {
console.log(this); // this > header
});
header.addEventListener('click', () => {
console.log(this); // this > window
});
b = a.bind(window); // a라는 함수에 this만 바꿔서 새로 생성.
b(); // 따로 호출해줘야 한다
function add(a, b) {
return a + b;
}
add.apply(null, [3, 5]); // 8 // 매개변수들을 배열로 넣음
function add(a, b) {
return a + b;
}
add.call(null, 3, 5); // 8 // 매개변수를 순서대로 넣음