1. hasOwnProperty() method
const hero = {
name: 'Batman'
};
hero.hasOwnProperty('name'); // => true
hero.hasOwnProperty('realName'); // => false
2. in operator
const hero = {
name: 'Batman'
};
'name' in hero; // => true
'realName' in hero; // => false
const hero = {
name: 'Batman'
};
hero.toString; // => function() {...}
'toString' in hero; // => true
hero.hasOwnProperty('toString'); // => false
3. Comparing with undefined
hero.name; // => 'Batman'
hero.realName; // => undefined
hero.realName evaluates to undefined because realName property is missing.
Now you can see an idea: you can compare with undefined to determine the existence of the property.
const hero = {
name: 'Batman'
};
hero.name !== undefined; // => true
hero.realName !== undefined; // => false
'[DEV] Programming Lang > TypeScript' 카테고리의 다른 글
[RxJs] debounceTime(milsec), distinctUntilChanged(), switchMap(), 실시간 검색 (0) | 2020.11.12 |
---|---|
[RxJs] unSubscribe() 3가지 기술 (0) | 2020.10.29 |
[ES6] Spread(...) 문법, 비구조화 할당 문법 (0) | 2020.10.23 |
[자주쓰는표현] Partial<T>와 Object.assign(origin_json, merge_json)를 활용 (0) | 2020.09.17 |
TypeScript 기초 (0) | 2020.07.09 |
최근댓글