자신이 속한 객체 혹은 자신이 생성할 인스턴스를 가리키는 자기 참조 변수다.
function Circle(radius) {
// 생성자 함수가 없으므로 자신이 생성할 인스턴스를 가리키는 식별자는 알 수 없다.
????.radius = radius;
}
Circle.prototype.getDiameter = function() {
// 여기도 마찬가지로 생성자 함수가 없으므로 식별자를 알 수 없다.
return 2 * ????.radius;
}
// 생성자 함수로 인스턴스를 생성하면서 가리키는 식별자가 생성됐다.
const circle = new Circle(5)
console.log(this); // window
function example() {
console.log(this); // window
}
const person = {
name: 'Lee',
getName() {
console.log(this); // {name: 'Lee', getName: f}
}
}
// 생성자 함수가 있어야 식별자 생성됨.
function Person(name) {
this.name = name;
console.log(this); // Person {name: 'Lee'}
}
const me = new Person('Lee');
여기에서는 undefined로 바인딩된다.
일반 함수 내부에서는 일반적으로 사용하지 않기 때문이다.
this는 Class에서 주로 사용되기 때문이다.