Is it possible to get the non-enumerable inherited property names of an object?

后端 未结 9 1746
温柔的废话
温柔的废话 2020-11-22 12:46

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

1) Object.keys(), which returns all own, enu

9条回答
  •  有刺的猬
    2020-11-22 13:34

    function getNonEnumerableNonOwnPropertyNames( obj ) {
        var oCurObjPrototype = Object.getPrototypeOf(obj);
        var arReturn = [];
        var arCurObjPropertyNames = [];
        var arCurNonEnumerable = [];
        while (oCurObjPrototype) {
            arCurObjPropertyNames = Object.getOwnPropertyNames(oCurObjPrototype);
            arCurNonEnumerable = arCurObjPropertyNames.filter(function(item, i, arr){
                return !oCurObjPrototype.propertyIsEnumerable(item);
            })
            Array.prototype.push.apply(arReturn,arCurNonEnumerable);
            oCurObjPrototype = Object.getPrototypeOf(oCurObjPrototype);
        }
        return arReturn;
    }
    

    Example of using:

    function MakeA(){
    
    }
    
    var a = new MakeA();
    
    var arNonEnumerable = getNonEnumerableNonOwnPropertyNames(a);
    

提交回复
热议问题