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
'개발 일반 > 프로그래밍 언어 ∕ JS & TS' 카테고리의 다른 글
Promise와 Observable 객체의 차이 (0) | 2020.10.28 |
---|---|
Promise, Async Await에 대한 고찰 (0) | 2020.10.28 |
[ES6] Spread(...) 문법, 비구조화 할당 문법 (0) | 2020.10.23 |
ISODate에 대한 고찰 (0) | 2020.10.08 |
[자주쓰는표현] Partial<T>와 Object.assign(origin_json, merge_json)를 활용 (0) | 2020.09.17 |
최근댓글