Is it possible to emulate non-enumerable properties?

前端 未结 3 1083
星月不相逢
星月不相逢 2020-12-13 18:25

ES5 has a enumerable flag. Example

Example

var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor
 , pd = getOwnPropertyDescriptor(Object.p         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-13 19:15

    Partial.js by Jake Verbaten is the answer to it.

    The partial.js is as follows

    /* partial non-enumerable property implementation
    
      Adds a flag to a weakmap saying on obj foo property bar is not enumerable.
    
      Then checks that flag in Object.keys emulation.
    */
    
    // pd.Name :- https://github.com/Raynos/pd#pd.Name
    var enumerables = pd.Name();
    
    Object.defineProperty = function (obj, name, prop) {
        if (prop.enumerable === false) {
             enumerables(obj)[name] = true;
        }
        ...
    };
    
    Object.keys = function (obj) {
        var enumerabilityHash = enumerables(obj), keys = [];
        for (var k in obj) {
            if (obj.hasOwnProperty(k) && !enumerabilityHash[k]) {
                 keys.push(k);
            }
        }
        return keys;
    };
    
    Object.getOwnPropertyNames = function (obj) {
        var keys = [];
        for (var k in obj) { 
            if (obj.hasOwnProperty(k)) {
                 keys.push(k);
            }
        }
    };
    

    I hope this helps the guys searching for this fix.

提交回复
热议问题