결론 : 'this'의 값은 함수가 어떻게 호출되었는지에 따라 달라진다.

this는 웹 브라우저 환경에서 기본적으로 Window 객체를 가리킨다.
함수의 this가 기본적으로 window인 동작 원리는 "실행 컨텍스트" 개념과 연관되어 있다.
(해당 내용은 공부하여 추후 업데이트 예정입니다.)
👉 링크
객체의 메서드로서 함수가 호출될 때, this는 그 객체 자체를 참조한다.
var obj = {
a: function() { console.log(this); }
};
obj.a(); // obj 객체 자체를 출력한다. = {a: ƒ}
(추가 예시)
var obj = {
name: "Kongveloper",
sayHello: function() { console.log("Hello, " + this.name); }
};
obj.sayHello(); // 출력: "Hello, Kongveloper"
호출 시, 호출하는 함수가 객체의 메서드인지, 그냥 함수인지의 여부가 중요하다.

메서드 bind, call, apply를 사용해서 명시적으로 this를 바꿀 수 있다.

function foo(num1, num2) {
console.log(this.name, num1 + num2);
}
const person = {
name: 'kong',
};
foo.call(person, 1, 2); // kong, 3
foo.apply(person, [1, 2]); // kong, 3
const newFoo = foo.bind(person);
newFoo(1, 2); // kong, 3
[ 생성자 함수를 new 키워드를 사용하지 않고 그냥 호출한 경우 ]

[ 생성자 함수를 new 키워드로 호출한 경우 ]

[Event Listener]

[ 문제 상황 ]
내부 inner 함수 호출 시, this는 window,
클릭 이벤트 핸들러 함수 호출 시, this는 'div 요소', 서로 다른 걸 참조한다.
$('div').on('click', function() {
console.log(this); // <div>
function inner() {
console.log('inner', this); // inner Window
}
inner();
});
[ 문제 해결 방법 ]
ES6에 화살표 함수 기능이 도입되기 이전엔, 아래의 예제와 같이 우회적으로 this를 회피하는 방법밖엔 없었다.
$('div').on('click', function() {
console.log(this); // <div>
var that = this;
function inner() {
console.log('inner', that); // inner <div>
}
inner();
});
2) ES6 : Arrow Function
화살표 함수 사용 시, this는 상위 함수의 this를 참조한다.
$('div').on('click', function() {
console.log(this); // <div>
const inner = () => {
console.log('inner', this); // inner <div>
}
inner();
});
[Summary]
자바스크립트에서 ' this '는 함수 호출 방식에 따라 달라진다. 기본적으로 함수의 this는 window를 참조하지만, 객체 메서드, bind, call, apply, new 키워드를 사용할 때 this의 참조가 바뀐다.
(cf. strict mode에서는 undefined를 가리킨다.)
이벤트 리스너, jQuery, React 등 기타 라이브러리에서 this를 내부적으로 바꾸는 경우가 있기 때문에, this 사용에 주의가 필요하다.