TIL 005

Chaeeun Lee·2025년 6월 10일
  • Array add elements
    • Array.prototype.push() 의 반환값은 Array의 length
    • Array.prototype.unshift() 의 반환값은 Array의 length
  • Array remove elements
    • Array.prototype.pop()의 반환값은 제거된 요소
    • Array.prototype.shift()의 반환값은 제거된 요소
  • this
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 객체 자신을 가리킨다.

profile
나는야 뚝딱이 개발자야

0개의 댓글