자바스크립트에서 this가 가리키는것은 현재 실행되고 있는 환경에 따라서 변화한다.
this가 객체를 가리키는 경우는 그 객체안에 있는 메소드가 실행되었을 때이다.
함수 안에서 this가 가리키는 건 메소드를 실행한 주체 객체이다.
중요한건 누가 실행한것인지??!
let person = {};
person.username ='영준';
person.age = 27;
person.introduce = function () {
console.log(`안녕? 나는 ${this.username}이야 나이는 ${this.age}살이야.`)
}
let person2 = {
name : 'alex',
age : 25,
introduce : function() {
console.log(`안녕? 나는 ${this.username}이야 나이는 ${this.age}살이야.`);
}
};
person.introduce(); // 여기서 this가 가리키는 건 person
person2.introduce(); // 여기서 this가 가리키는 건 person2