Las propiedades enumerables son aquellas propiedades cuyo indicador enumerable interno se establece en true
, que es el valor predeterminado para las propiedades creadas mediante una asignación simple o mediante un iniciador de propiedad (propiedades definidas mediante Object.defineProperty
y tal valor enumerable predeterminado a false
). Se muestran numerosas propiedades en bucles
a menos que la clave de la propiedad sea
Symbol
. La posesión de las propiedades está determinada por si la propiedad pertenece directamente al objeto y no a su cadena prototipo. Las propiedades de un objeto también se pueden recuperar en total. Hay varios medios incorporados para detectar, iterar/enumerar y recuperar propiedades de objetos, y el gráfico que se muestra a continuación está disponible. A continuación, se muestra un código de muestra que demuestra cómo obtener las categorías faltantes.
Propiedad, enumerabilidad y posesión — métodos integrados de detección, recuperación e iteración
Funcionalidad |
Propia del Objeto |
Propia del Objeto y su cadena prototipo |
Solo en cadena prototipo |
Detección |
|
Enumerable |
No enumerable |
Enumerable y no enumerable |
No disponible sin código adicional |
No disponible sin código adicional |
in |
|
No disponible sin código adicional |
Recuperación |
|
No disponible sin código adicional |
No disponible sin código adicional |
Iterable |
|
Enumerable |
No enumerable |
Enumerable y no enumerable |
for..in
(no incluye símbolos)
|
No disponible sin código adicional |
No disponible sin código adicional |
|
No disponible sin código adicional |
Ten en cuenta que este no es el algoritmo más eficiente para todos los casos, pero es útil para una demostración rápida.
- La detección puede ocurrir por
SimplePropertyRetriever.theGetMethodYouWant(obj).indexOf(prop) > -1
- La iteración puede ocurrir por
SimplePropertyRetriever.theGetMethodYouWant(obj).forEach(function (value, prop) {});
(o usa filter()
, map()
, etc.)
var SimplePropertyRetriever = {
getOwnEnumerables: function(obj) {
return this._getPropertyNames(obj, true, false, this._enumerable);
},
getOwnNonenumerables: function(obj) {
return this._getPropertyNames(obj, true, false, this._notEnumerable);
},
getOwnEnumerablesAndNonenumerables: function(obj) {
return this._getPropertyNames(obj, true, false, this._enumerableAndNotEnumerable);
},
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);
},
getOwnAndPrototypeNonenumerables: function(obj) {
return this._getPropertyNames(obj, true, true, this._notEnumerable);
},
getOwnAndPrototypeEnumerablesAndNonenumerables: function(obj) {
return this._getPropertyNames(obj, true, true, this._enumerableAndNotEnumerable);
},
_enumerable: function(obj, prop) {
return obj.propertyIsEnumerable(prop);
},
_notEnumerable: function(obj, prop) {
return !obj.propertyIsEnumerable(prop);
},
_enumerableAndNotEnumerable: function(obj, prop) {
return true;
},
_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;
}
};