6.5 this 키워드

  • 객체의 프로퍼티인 함수에서 의미가 있음

  • 메서드를 호출하면 this는 호출한 메서드를 소유하는 객체가 됨

  • 중첩된 함수 안에서 this를 사용하면 의도한대로 동작하지 않음 --> this를 다른 변수에 할당

  • 그러므로 this를 self나 that에 할당하는 코드가 많음

const o2 = {
    name: 'Wallance',
    bark: function() { return "Woof! my name is " + this.name }
}

console.log(o2.bark()); // Woof! my name is Wallance

Last updated