Object.getOwnPropertyNames()
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.getOwnPropertyNames() ë©ìëë ì ë¬ë ê°ì²´ì 모ë ìì± (ì¬ë³¼ì ì¬ì©íë ìì±ì ì ì¸í ì´ê±°í ì ìë ìì± í¬í¨) ë¤ì ë°°ì´ë¡ ë°íí©ëë¤.
ìëí´ ë³´ê¸°
const object1 = {
a: 1,
b: 2,
c: 3,
};
console.log(Object.getOwnPropertyNames(object1));
// Expected output: Array ["a", "b", "c"]
구문
Object.getOwnPropertyNames(obj);
매ê°ë³ì
obj-
ë°í ë°ì ì´ê±°í ìì±ê³¼ ì´ê±°íì´ ìë ìì±ì ê°ì§ ê°ì²´
ë°í ê°
ì ë¬ë ê°ì²´ì ìë ìì±ë¤ì 문ìì´ ë°°ì´ì ë°íí©ëë¤.
ì¤ëª
Object.getOwnPropertyNames() ë ì ë¬ë ê°ì²´(obj)ì ì´ê±°í ë° ì´ê±°í ì ìë ìì±ë¤ì 문ìì´ ë°°ì´ë¡ ë°íí©ëë¤.
ë°°ì´ì ì´ê±°í ì ìë ìì±ë¤ì ììë for...in ë°ë³µë¬¸ (ëë Object.keys())ì´ ì²ë¦¬ëë ììì ì¼ì¹í©ëë¤.
ES6 문ë²ì ë°ë¼, ê°ì²´ì ì ìí í¤ (ì´ê±°í ë° ë¹-ì´ê±°í í¬í¨)ê° ë¨¼ì ë°°ì´ì ì¤ë¦ì°¨ìì¼ë¡ ì¶ê°ë ë¤ì 문ìì´ í¤ë¥¼ ì½ì
íë ììë¡ ì¶ê°ë©ëë¤.
ES5ììë ì¸ì(obj)ê° ê°ì²´ê° ìë ê²½ì° (ìì íì
) TypeError ê° ë°ìí©ëë¤.
ES2015ììë, ê°ì²´ê° ìë ì¸ì를 ê°ì²´ íì
ì¼ë¡ ê°ì íë³íí©ëë¤.
Object.getOwnPropertyNames("foo");
// TypeError: "foo" is not an object (ES5 code)
Object.getOwnPropertyNames("foo");
// ["0", "1", "2", "length"] (ES2015 code)
ìì
>Using Object.getOwnPropertyNames()
var arr = ["a", "b", "c"];
console.log(Object.getOwnPropertyNames(arr).sort()); // .sort() ë ë°°ì´ ë©ìëì
ëë¤.
// logs ["0", "1", "2", "length"]
// ë°°ì´í ê°ì²´
var obj = { 0: "a", 1: "b", 2: "c" };
console.log(Object.getOwnPropertyNames(obj).sort()); // .sort() ë ë°°ì´ ë©ìëì
ëë¤.
// logs ["0", "1", "2"]
// ìì± ëª
ê³¼ ìì± ê°ì Array.forEach ë©ìë를 ì¬ì©íì¬ ë¡ê¹
í©ëë¤.
Object.getOwnPropertyNames(obj).forEach(function (val, idx, array) {
console.log(val + " -> " + obj[val]);
});
// logs
// 0 -> a
// 1 -> b
// 2 -> c
// ì´ê±°í ì ìë ìì±
var my_obj = Object.create(
{},
{
getFoo: {
value: function () {
return this.foo;
},
enumerable: false,
},
},
);
my_obj.foo = 1;
console.log(Object.getOwnPropertyNames(my_obj).sort());
// logs ["foo", "getFoo"]
ë§ì½ ì´ê±° ê°ë¥í ìì±ë§ ì¬ì©íë¤ë©´, Object.keys() ëë for...in ë°ë³µë¬¸ì ì¬ì©íë걸 ê¶ì¥í©ëë¤.
(ì´ë ê°ì²´ì íë¡í íì
ì²´ì¸ì 먼ì ì¬ì©íì¬ ì´ê±° ê°ë¥í ìì±ì ë°íí©ëë¤. ë¨, íìëhasOwnProperty()ì íí° ë©ëë¤.)
íë¡í íì ì²´ì¸ì ìë ììë¤ì ëì´ëì§ ìì:
function ParentClass() {}
ParentClass.prototype.inheritedMethod = function () {};
function ChildClass() {
this.prop = 5;
this.method = function () {};
}
ChildClass.prototype = new ParentClass();
ChildClass.prototype.prototypeMethod = function () {};
console.log(
Object.getOwnPropertyNames(
new ChildClass(), // ["prop", "method"]
),
);
ì´ê±°í ì ìë ìì±ë§ ê°ì ¸ì¤ê¸°
ì´ ë°©ë²ì Array.prototype.filter() í¨ì를 ì¬ì©í´ (Object.getOwnPropertyNames() ì íµí´ ì»ì) 모ë í¤ ì¤ (Object.keys() ì íµí´ ì»ì) ì´ê±° ê°ë¥í í¤ë¤ì ì ê±°íì¬ ì´ê±°í ì ìë í¤ë¤ë§ ì¶ë ¥í©ëë¤.
var target = myObject;
var enum_and_nonenum = Object.getOwnPropertyNames(target);
var enum_only = Object.keys(target);
var nonenum_only = enum_and_nonenum.filter(function (key) {
var indexInEnum = enum_only.indexOf(key);
if (indexInEnum == -1) {
// enum_only ì í¤ ê°ì´ ìë¤ë ê²ì
// ê·¸ í¤ê° ì´ê±°í ì ìë í¤ ìì ì미í©ëë¤.
// ê·¸ëì ì´ íí°ìì true를 ë°íí©ëë¤.
return true;
} else {
return false;
}
});
console.log(nonenum_only);
ëª ì¸
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-object.getownpropertynames> |
ë¸ë¼ì°ì í¸íì±
ê°ì´ 보기
Object.getOwnPropertyNamesì í´ë¦¬í ì½ëë ìëìì íì¸ í ì ììµëë¤.core-js- Enumerability and ownership of properties
Object.prototype.hasOwnProperty()Object.prototype.propertyIsEnumerable()Object.create()Object.keys()Array.forEach()