Enumerability and ownership of properties
'Enumerable properties'(열거 가능한 속성)는 내부 열거 형 플래그가 true로 설정된 property로, 이는 간단한 할당 또는 property initializer (Object.defineProperty를 통해 정의 된 특성 및 이러한 기본 열거 형을 false로 정의한 특성)를 통해 작성된 property의 기본값입니다.
등록 정보의 키가 Symbol이 아니면 열거 가능한 등록 정보가 for...in 루프에 표시됩니다. 'Ownership of properties' (속성의 소유권)은 속성이 프로토 타입 체인이 아닌 개체에 직접 속하는지 여부에 따라 결정됩니다. 객체의 속성도 전체적으로 검색 할 수 있습니다. 개체 속성을 감지, 반복 / 열거 및 검색하는 여러 가지 기본 제공 방법이 있으며 아래 표와 같이 사용할 수 있습니다. 누락 된 범주를 얻는 방법을 보여주는 샘플 코드는 다음과 같습니다.
객체 속성 감지, 검색 및 열거
개체 속성을 감지, 반복/열거 및 검색하는 여러 가지 기본 제공 수단이 있습니다. 아래 표에 요약되어 있습니다.
Detection
Own object | Own object and prototype chain | Prototype chain only | |
---|---|---|---|
Enumerable | Not available without extra code | Not available without extra code | |
Nonenumerable |
|
Not available without extra code | Not available without extra code |
Enumerable and Nonenumerable | in |
Not available without extra code |
Retrieval
Own object | Own object and prototype chain | Prototype chain only | |
---|---|---|---|
Enumerable | Not available without extra code | Not available without extra code | |
Nonenumerable |
getOwnPropertyNames ,
getOwnPropertySymbols
– filtered to exclude enumerables using propertyIsEnumerable
|
Not available without extra code | Not available without extra code |
Enumerable and Nonenumerable | Not available without extra code | Not available without extra code |
Iteration
Own object | Own object and prototype chain | Prototype chain only | |
---|---|---|---|
Enumerable |
(excluding symbols) |
Not available without extra code | |
Nonenumerable |
getOwnPropertyNames ,
getOwnPropertySymbols
– filtered to exclude enumerables using propertyIsEnumerable
|
Not available without extra code | Not available without extra code |
Enumerable and Nonenumerable | Not available without extra code | Not available without extra code |
Obtaining properties by enumerability/ownership
아래는 모든 경우에 가장 효율적인 알고리즘은 아니지만 빠르게 코드를 작성하여 확인하기 좋습니다.
- Detection can occur by
SimplePropertyRetriever.theGetMethodYouWant(obj).indexOf(prop) > -1
- Iteration can occur by
SimplePropertyRetriever.theGetMethodYouWant(obj).forEach(function (value, prop) {});
(or usefilter()
,map()
, etc.)
var SimplePropertyRetriever = {
getOwnEnumerables: function(obj) {
return this._getPropertyNames(obj, true, false, this._enumerable);
// Or could use for..in filtered with hasOwnProperty or just this: return Object.keys(obj);
},
getOwnNonenumerables: function(obj) {
return this._getPropertyNames(obj, true, false, this._notEnumerable);
},
getOwnEnumerablesAndNonenumerables: function(obj) {
return this._getPropertyNames(obj, true, false, this._enumerableAndNotEnumerable);
// Or just use: return Object.getOwnPropertyNames(obj);
},
getPrototypeEnumerables: function(obj) {
return this._getPropertyNames(obj, false, true, this._enumerable);
},
getPrototypeNonenumerables: function(obj) {
return this._getPropertyNames(obj, false, true, this._notEnumerable);
},
getPrototypeEnumerablesAndNonenumerables: function(obj) {
return this._getPropertyNames(obj, false, true, this._enumerableAndNotEnumerable);
},
getOwnAndPrototypeEnumerables: function(obj) {
return this._getPropertyNames(obj, true, true, this._enumerable);
// Or could use unfiltered for..in
},
getOwnAndPrototypeNonenumerables: function(obj) {
return this._getPropertyNames(obj, true, true, this._notEnumerable);
},
getOwnAndPrototypeEnumerablesAndNonenumerables: function(obj) {
return this._getPropertyNames(obj, true, true, this._enumerableAndNotEnumerable);
},
// Private static property checker callbacks
_enumerable: function(obj, prop) {
return obj.propertyIsEnumerable(prop);
},
_notEnumerable: function(obj, prop) {
return !obj.propertyIsEnumerable(prop);
},
_enumerableAndNotEnumerable: function(obj, prop) {
return true;
},
// Inspired by http://stackoverflow.com/a/8024294/271577
_getPropertyNames: function getAllPropertyNames(obj, iterateSelfBool, iteratePrototypeBool, includePropCb) {
var props = [];
do {
if (iterateSelfBool) {
Object.getOwnPropertyNames(obj).forEach(function(prop) {
if (props.indexOf(prop) === -1 && includePropCb(obj, prop)) {
props.push(prop);
}
});
}
if (!iteratePrototypeBool) {
break;
}
iterateSelfBool = true;
} while (obj = Object.getPrototypeOf(obj));
return props;
}
};
Detection Table
Enumerable | Nonenumerable | Symbols keys | Inherited Enumerable | Inherited Nonenumerable | Inherited Symbols keys | |
---|---|---|---|---|---|---|
in |
true | true | true | true | true | true |
for..in |
true | false | false | true | false | false |
obj.hasOwnProperty |
true | true | true | false | false | false |
obj.propertyIsEnumerable |
true | false | true | false | false | false |
Object.keys |
true | false | false | false | false | false |
Object.getOwnPropertyNames |
true | true | false | false | false | false |
Object.getOwnPropertyDescriptors |
true | true | true | false | false | false |
Reflect.ownKeys() |
true | true | true | false | false | false |