JavaScript์์ ๊ฐ์ฒด์ ํน์ ํค(์์ฑ)๊ฐ ์กด์ฌํ๋์ง ํ์ธํ๋ ๋ค์ํ ๋ฐฉ๋ฒ์ ๋ํด ํ์ตํ์ต๋๋ค.
const obj = { name: "Alice", age: 25 };
console.log(obj.hasOwnProperty("name")); // true
console.log(obj.hasOwnProperty("gender")); // false
const obj = { name: "Alice", age: 25 };
console.log("name" in obj); // true
console.log("toString" in obj); // true (ํ๋กํ ํ์
์์ ์์)
const obj = { name: "Alice", age: 25 };
console.log(obj?.name !== undefined); // true
console.log(obj?.gender !== undefined); // false
function hasKey(obj, key) {
return obj.hasOwnProperty(key);
}
function hasKeyIncludingPrototype(obj, key) {
return key in obj;
}
hasOwnProperty์ in ์ฐ์ฐ์์ ์ฐจ์ด์
Modern JavaScript์ ํธ๋ฆฌํ ๊ธฐ๋ฅ
์ด์ ๋ ๊ฐ์ฒด์ ํค๋ฅผ ํ์ธํ ๋ ์ํฉ์ ๋ง๋ ์ ์ ํ ๋ฐฉ๋ฒ์ ์ ํํ ์ ์๊ฒ ๋๋ค! ๐ฏ