是否可以获取对象的不可枚举的继承属性名称? [英] Is it possible to get the non-enumerable inherited property names of an object?

查看:175
本文介绍了是否可以获取对象的不可枚举的继承属性名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中,我们有几种获取对象属性的方法,具体取决于我们想要获得的内容。

In JavaScript we have a few ways of getting the properties of an object, depending on what we want to get.

1) Object.keys(),返回对象的所有自身可枚举属性,ECMA5方法。

1) Object.keys(), which returns all own, enumerable properties of an object, an ECMA5 method.

2)a for ... in 循环,返回对象的所有可枚举属性,无论如何它们是属于自己的属性,还是从原型链继承而来。

2) a for...in loop, which returns all the enumerable properties of an object, regardless of whether they are own properties, or inherited from the prototype chain.

3) Object.getOwnPropertyNames(obj)返回对象的所有属性,可枚举与否。

3) Object.getOwnPropertyNames(obj) which returns all own properties of an object, enumerable or not.

我们还有 hasOwnProperty(prop)让我们检查属性是继承还是实际属于该对象,以及 propertyIsEnumerable(prop),顾名思义,它让我们检查属性是否可枚举。

We also have such methods as hasOwnProperty(prop) lets us check if a property is inherited or actually belongs to that object, and propertyIsEnumerable(prop) which, as the name suggests, lets us check if a property is enumerable.

有了所有这些选项,就无法获得对象的不可枚举,非自己的属性,这就是我的意思想做。有没有办法做到这一点?换句话说,我可以以某种方式获得继承的非可枚举属性的列表吗?

With all these options, there is no way to get a non-enumerable, non-own property of an object, which is what I want to do. Is there any way to do this? In other words, can I somehow get a list of the inherited non-enumerable properties?

谢谢。

推荐答案

由于 getOwnPropertyNames 可以获得不可枚举的属性,您可以使用它并将其与走原型链相结合。

Since getOwnPropertyNames can get you non-enumerable properties, you can use that and combine it with walking up the prototype chain.

function getAllProperties(obj){
    var allProps = []
      , curr = obj
    do{
        var props = Object.getOwnPropertyNames(curr)
        props.forEach(function(prop){
            if (allProps.indexOf(prop) === -1)
                allProps.push(prop)
        })
    }while(curr = Object.getPrototypeOf(curr))
    return allProps
}

我在Safari 5.1上进行了测试并得到了

I tested that on Safari 5.1 and got

> getAllProperties([1,2,3])
["0", "1", "2", "length", "constructor", "push", "slice", "indexOf", "sort", "splice", "concat", "pop", "unshift", "shift", "join", "toString", "forEach", "reduceRight", "toLocaleString", "some", "map", "lastIndexOf", "reduce", "filter", "reverse", "every", "hasOwnProperty", "isPrototypeOf", "valueOf", "__defineGetter__", "__defineSetter__", "__lookupGetter__", "propertyIsEnumerable", "__lookupSetter__"]






更新:稍微重构了代码(添加了空格和花括号,并改进了函数名称):


Update: Refactored the code a bit (added spaces, and curly braces, and improved the function name):

function getAllPropertyNames( obj ) {
    var props = [];

    do {
        Object.getOwnPropertyNames( obj ).forEach(function ( prop ) {
            if ( props.indexOf( prop ) === -1 ) {
                props.push( prop );
            }
        });
    } while ( obj = Object.getPrototypeOf( obj ) );

    return props;
}

简单地获取所有内容..(enum / nonenum,self / inherited ..
请确认..

function getAllPropertyNames( obj ) {
    var props = [];

    do {
        props= props.concat(Object.getOwnPropertyNames( obj ));
    } while ( obj = Object.getPrototypeOf( obj ) );

    return props;
}

这篇关于是否可以获取对象的不可枚举的继承属性名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆