JavaScript

[모던 JavaScript 튜토리얼] '객체를 원시형으로 변환하기' 예시 코드

선인장777 2023. 6. 6. 11:22
let jihye = {
    family: 'daughter', 
    payment: 8950,
	[Symbol.toPrimitive](hint){
        return hint == 'string' ? this.family : this.payment;
    }
}

alert(`jihye는 ${Number(jihye)}이다`);

 

hint 자체가 (typeOf로 비교하는 것이 아닌) 'string' 또는 'number'이므로 별도의 typeOf를 안써도 비교 가능