const mark = {
fullName : 'Mark Miller',
mass : 78,
height : 1.78,
calcBMI : () => {
let result = this.mass / (this.height * this.height)
this.bmi = result
return result
}
}
이렇게 calcBMI 를 화살표 함수로 작성하게 되면 undefined 오류가 발생하게 된다.
💡 왜냐하면 화살표 함수의 this는 자신의 객체가 아닌, 상위 스코프를 참조하기 때문이다.
오류를 해결하려면 calcBMI 메서드를 일반 함수로 작성해야 한다.
const mark = {
fullName : 'Mark Miller',
mass : 78,
height : 1.78,
calcBMI : function () {
let result = this.mass / (this.height * this.height)
this.bmi = result
return result
}
}
💡 일반 함수의 this는 mark 객체 자신을 가리킨다.