This란
this는 자신이 속한 객체 또는 자신이 생성할 인스턴스를 가리키는 자기 참조 변수이다.
this는 선언이 아닌 호출할 때 결정되는 것이다.
전역범위에서 사용 될 때는 전역객체를 가리킨다.
console.log(this === window); //true
a = 10;
console.log(window.a); //10
function b(){
return this;
}
b() === window; // true
객체에 속한 메서드에서 사용될 때는 그 메서드의 객체(점 앞에 명시된 객체)를 가리킨다.
let may = {
firstName: "May",
lastName: "Lee",
love(){
console.log(this.firstName + "loves Dog")
}
}
may.love(); // May loves Dog
생성자에서 사용될 때 생성자로 인해 생성된 새로운 객체를 가리킨다.
function person(name){
this.name = name;
}
}
let newPerson = new person("Fall");
console.log(newPerson.name); // "Fall"
apply, call 메서드를 사용하여 this에 바인딩 될 값을 명시적으로 구현 할 수 있다.
참고자료
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Operators/this