Object.prototype.hasOwnProperty()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since 2015ë 7ì.
Object ì¸ì¤í´ì¤ì hasOwnProperty() ë©ìëë
í´ë¹ ê°ì²´ ìì²´ì ê³ ì í ìì±ì¸ì§ (ìì ë°ì ìì±ì´ ìëì§) ëíë´ë ë¶ë¦¬ì¸ ê°ì ë°íí©ëë¤.
Note:
Object.hasOwn()ê° ê¶ì¥ë©ëë¤.hasOwnProperty()ë ì´ë¥¼ ì§ìíë ë¸ë¼ì°ì ììë§ ì¬ì©ë©ëë¤.
ìëí´ ë³´ê¸°
const object1 = {};
object1.property1 = 42;
console.log(object1.hasOwnProperty("property1"));
// Expected output: true
console.log(object1.hasOwnProperty("toString"));
// Expected output: false
console.log(object1.hasOwnProperty("hasOwnProperty"));
// Expected output: false
구문
hasOwnProperty(prop)
íë¼ë¯¸í°
ë°í ê°
ê°ì²´ê° í¹ì ìì±ì ê³ ì í ìì±ì¼ë¡ ê°ì§ê³ ìë¤ë©´ true 를 ë°ííê³ ê·¸ë ì§ ìì¼ë©´ false 를 ë°íí©ëë¤.
ì¤ëª
hasOwnProperty() ë©ìëë í¹ì ìì±ì´ í´ë¹ ê°ì²´ì ê³ ì í ìì±ì´ë¼ë©´ ì´ ê°ì´ null í¹ì undefined ì¼ì§ë¼ë true 를 ë°íí©ëë¤. ì´ ìì±ì´ ìì ë°ì ìì±ì´ê±°ë ì´ëììë ì ìëì§ ììë¤ë©´ false 를 ë°íí©ëë¤. in ì°ì°ììë ë³ê°ë¡, ì´ ë©ìëë í¹ì ìì±ì´ í´ë¹ ê°ì²´ì íë¡í íì
ì²´ì¸ìì ì§ì ë ê²ì¸ì§ ê²ì¦íì§ ììµëë¤.
ì´ ë©ìëë ëë¤ìì JavaScript ê°ì²´ìì í¸ì¶ë ì ììµëë¤. ë§ì ê°ì²´ê° Object ìì íìëìê³ , ì´ ë©ìë를 ììë°ê¸° ë문ì
ëë¤. ì를 ë¤ì´ Arrayë Objectì´ê¸° ë문ì hasOwnProperty() ë©ìë를 ì¬ì©íì¬ ì¸ë±ì¤ê° ì¡´ì¬íëì§ íì¸í ì ììµëë¤.
const fruits = ["Apple", "Banana", "Watermelon", "Orange"];
fruits.hasOwnProperty(3); // true ('Orange')
fruits.hasOwnProperty(4); // false - not defined
ì´ ë©ìëë ì¬êµ¬ì±ë ê°ì²´ í¹ì Object.prototype ìì ììëì§ ìì null íë¡í íì
ê°ì²´ììë ì í¨íì§ ììµëë¤.
ìì
>hasOwnProperty를 íì©íì¬ ê³ ì ìì± ì¡´ì¬ ì¬ë¶ë¥¼ í ì¤í¸í기
ìë ì½ëë example ê°ì²´ê° prop ì´ë¼ë ì´ë¦ì ìì±ì í¬í¨íê³ ìëì§ íì¸íë ë°©ë²ì ë³´ì¬ì¤ëë¤.
const example = {};
example.hasOwnProperty("prop"); // false
example.prop = "exists";
example.hasOwnProperty("prop"); // true - 'prop' ì´ ì ìëì´ ììµëë¤.
example.prop = null;
example.hasOwnProperty("prop"); // true - ê³ ì ìì±ì´ null ê°ì¼ë¡ ì¡´ì¬í©ëë¤.
example.prop = undefined;
example.hasOwnProperty("prop"); // true - ê³ ì ìì±ì´ undefined ê°ì¼ë¡ ì¡´ì¬í©ëë¤.
ì§ì ìì± vs. ììë ìì±
ìë ìì ë ì§ì ì ì¸ ìì±ê³¼ íë¡í íì ì²´ì¸ì íµí´ ììë ìì± ì¬ì´ì ì°¨ì´ì ì ë³´ì¬ì¤ëë¤.
const example = {};
example.prop = "exists";
// `hasOwnProperty` ì§ì ìì±ììë trueë§ ë°íí©ëë¤.
example.hasOwnProperty("prop"); // true
example.hasOwnProperty("toString"); // false
example.hasOwnProperty("hasOwnProperty"); // false
// `in` ì°ì°ìë ì§ì ìì±ì´ë ììë ìì±ìì 모ë true를 ë°íí©ëë¤.
"prop" in example; // true
"toString" in example; // true
"hasOwnProperty" in example; // true
ê°ì²´ì ìì± ë°ë³µ ì²ë¦¬í기
ì´ ìì ë ììë ìì±ì ì¤ííì§ ìê³ ê°ì²´ì ì´ê±° ê°ë¥í ìì±ì ë°ë³µíì¬ ì²ë¦¬íë ë°©ë²ì ë³´ì¬ì¤ëë¤.
const buz = {
fog: "stack",
};
for (const name in buz) {
if (buz.hasOwnProperty(name)) {
console.log(`this is fog (${name}) for sure. Value: ${buz[name]}`);
} else {
console.log(name); // toString í¹ì ë¤ë¥¸ ê²ë¤
}
}
for...in 루íë ì´ê±° ê°ë¥í ìì´í
ë§ì ë°ë³µíë¤ë ì ì ì ìíì¸ì. ì´ê±° ê°ë¥íì§ ìì ìì±ë¤ì´ ëì¤ì§ ìëë¤ê³ í´ì hasOwnProperty ê° ì´ê±° ê°ë¥í í목ìë§ ì ì©ëë¤ë ê²ì ì미íì§ë ììµëë¤. Object.getOwnPropertyNames() 를 ì¬ì©íë¤ë©´ ì´ê±° ê°ë¥íì§ ìì ìì±ë¤ë ë°ë³µ ì²ë¦¬í ì ììµëë¤.
hasOwnProperty를 ìì± ì´ë¦ì¼ë¡ ì¬ì©í기
JavaScriptë ìì± ì´ë¦ hasOwnProperty ì ë³´í¸íì§ ììµëë¤. ì´ ì´ë¦ì ìì±ì ê°ì§ê³ ìë ê°ì²´ë ë¶ì í©í 결과를 ë°íí ìë ììµëë¤.
const foo = {
hasOwnProperty() {
return false;
},
bar: "Here be dragons",
};
foo.hasOwnProperty("bar"); // ì¬êµ¬ì±ë ê°ì²´ë íì false를 ë°íí©ëë¤.
ì´ ë¬¸ì 를 í´ê²°í기 ìí ê°ì¥ ê¶ì¥ëë ë°©ë²ì Object.hasOwn() 를 ì§ìíë ë¸ë¼ì°ì ìì ì´ë¥¼ ì¬ì©íë ë°©ë²ì
ëë¤. í¹ì ì¸ë¶ hasOwnProperty 를 ì¬ì©íë ëìë ììµëë¤.
const foo = { bar: "Here be dragons" };
// Object.hasOwn() ë©ìë를 ì¬ì©í©ëë¤. - ê¶ì¥
Object.hasOwn(foo, "bar"); // true
// Object íë¡í íì
ìì hasOwnProperty ìì±ì ì¬ì©í기
Object.prototype.hasOwnProperty.call(foo, "bar"); // true
// ë¤ë¥¸ Objectì hasOwnProperty를 ì¬ì©íê³
// 'this'ì callì í¸ì¶íì¬ fooì í ë¹í©ëë¤.
({}).hasOwnProperty.call(foo, "bar"); // true
ì²ì ë ê°ì§ ê²½ì°ìë ìë¡ ìì±ë ê°ì²´ê° ìë¤ë ì ì ì ìí´ì¼ í©ëë¤.
Object.create(null)ë¡ ìì±ë ê°ì²´
null íë¡í íì
ê°ì²´ë Object.prototype ë¡ë¶í° ììë ê²ì´ ìë기 ë문ì hasOwnProperty() ì ì ê·¼í ì ììµëë¤.
const foo = Object.create(null);
foo.prop = "exists";
foo.hasOwnProperty("prop"); // Uncaught TypeError: foo.hasOwnProperty is not a function
ì´ ê²½ì°ì í´ê²°ì±
ì ì´ ì¹ì
ìì íì¸íë ê²ê³¼ ëì¼í©ëë¤. 참조를 ìí´ Object.hasOwn() 를 ì¬ì©íê±°ë ì¸ë¶ ê°ì²´ìì hasOwnProperty() 를 ì¬ì©í ìë ììµëë¤.
ëª ì¸ì
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-object.prototype.hasownproperty> |