Object 판별

Array.prototype·2022년 6월 17일
0

자바스크립트의 최상위는 Object라서 {} 값인지 판별하기가 어려운데 constructor를 이용하면 판별이 가능하다.

const obj = {}
const str = 'str'
const num = 1
const arr = []
const nullish = null
const undefine = undefined
const map = new Map()
const set = new Set()

obj.constructor // ƒ Object() { [native code] }
str.constructor // ƒ String() { [native code] }
num.constructor // ƒ Number() { [native code] }
arr.constructor // ƒ Array() { [native code] }
nullish?.constructor // null은 constructor가 없으므로 옵셔널체이닝필요
undefine.constructor // undefined는 constructor가 없으므로 옵셔널체이닝 필요
map.constructor // ƒ Map() { [native code] }
set.constructor // ƒ Set() { [native code] }
profile
frontend developer

0개의 댓글