属性的可枚举性和所有权

可枚举属性是指那些可以通过 for..in 循环 循环遍历到的和属性.

属性所有权是由该属性是当前对象的自身属性还是当前对象从原型链上继承的属性来决定的.

Properties of an object can also be retrieved in total.

There are a number of built-in means of detecting, iterating/enumerating, and retrieving object properties, with the chart showing which are available. Some sample code follows which demonstrates how to obtain the missing categories.

 

属性的可枚举行和所有权 - 内置的判断,访问和迭代方法
功能 自身对象 自身对象及其原型链 仅原型链
判断
Enumerable Nonenumerable Enumerable and Nonenumerable
in and hasOwnProperty in and not propertyIsEnumerable hasOwnProperty
Not available without extra code Not available without extra code
访问
Enumerable Nonenumerable Enumerable and Nonenumerable
Object.keys getOwnPropertyNames filtered to include properties when not passing propertyIsEnumerable getOwnPropertyNames
Not available without extra code Not available without extra code
迭代
Enumerable Nonenumerable Enumerable and Nonenumerable
for..in filtered by hasOwnProperty Iterate over getOwnPropertyNames filtered to include properties when not passing propertyIsEnumerable Iterate over getOwnPropertyNames
Enumerable Nonenumerable Enumerable and Nonenumerable
for..in Not available without extra code Not available without extra code
Not available without extra code

通过可枚举型和所有权获取对象的属性

Note that this is not the most efficient algorithm for all cases, but useful for a quick demonstration.

  • Detection can occur bySimplePropertyRetriever.theGetMethodYouWant(obj).indexOf(prop) > -1
  • Iteration can occur bySimplePropertyRetriever.theGetMethodYouWant(obj).forEach(function (value, prop) {}); (or use filter()map(), etc.)
// Note that this is not the most efficient algorithm for all cases, but useful for a quick demonstration
// Detection can occur by SimplePropertyRetriever.theGetMethodYouWant(obj).indexOf(prop) > -1
// Iteration can occur by SimplePropertyRetriever.theGetMethodYouWant(obj).forEach(function (value, prop) {}); // Or use filter(), 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;
    }
};

统计表

  in hasOwnProperty propertyIsEnumerable 在Object.keys返回结果中 在Object.getOwnPropertyNames返回结果中
可枚举自身属性 true true true true true
不可枚举自身属性 true true false false true
可枚举继承属性 true false false false false
不可枚举继承属性 true false false false false

相关链接

 

文档标签和贡献者

最后编辑者: yenshen,
隐藏侧边栏