this: 자신이 속한 객체 또는 생성할 인스턴스(프로퍼티 및 메서드)를 가리키는 자기 참조 변수// this는 어디서든 참조 가능
// 1. 전역 this: 전역 객체 window
console.log(this); // window
function square(number) {
// 2. 일반 함수 내부 this: 전역 객체 window
console.log(this); // window
return number * number;
}
square(2);
const person = {
name: 'Lee',
getName() {
// 3. 메서드 내부 this: 메서드 호출한 객체
console.log(this); // {name: 'Lee', getName: f}
return this.name;
}
}
console.log(person.getName()); // Lee
function Person(name) {
// 4. 생성자 함수 내부 this: 생성할 인스턴스
this.name = name;
console.log(this); // Person {name: 'Lee'}
}
const me = new Person('Lee');
[출처] 모던 자바스크립트, Deep Dive