분류
javascript
Javascript 30초 Snippet - Type : is
본문
제공된 값이 지정된 유형인지 확인합니다.
https://github.com/30-seconds/30-seconds-of-code
Array.prototype.includes()를 사용하여 값이 undefined가 아니거나 null이 아닌지 확인하고 값의 생성자 특성을 유형과 비교하여 제공된 값이 지정된 유형인지 확인하십시오.
const is = (type, val) => ![, null].includes(val) && val.constructor === type;
ex)
is(Array, [1]); // true is(ArrayBuffer, new ArrayBuffer()); // true is(Map, new Map()); // true is(RegExp, /./g); // true is(Set, new Set()); // true is(WeakMap, new WeakMap()); // true is(WeakSet, new WeakSet()); // true is(String, ''); // true is(String, new String('')); // true is(Number, 1); // true is(Number, new Number(1)); // true is(Boolean, true); // true is(Boolean, new Boolean(true)); // true
- 이전글Javascript 30초 Snippet - Type : isArrayLike 19.11.25
- 다음글Javascript 30초 Snippet - Type : getType 19.11.25