How to add non-enumerable property in JavaScript f

2019-08-06 20:03发布

问题:

Is there a way to add "hidden" non-enumerable properties to a JavaScript object that works cross-browser?

For most modern browsers, you can do:

Object.defineProperty(obj, '__id__', { enumerable: false, value: id++ });

For some older non-IE browsers that don't have Object.defineProperty, you can use the __proto__ hack.

None of those, however, work for IE. Is there a way to accomplish this in IE8 (would be cool if IE7 too, but not necessary)?

The main goal is to be able to add tracker properties to any JavaScript {} object, but such that when you call JSON.stringify(obj), it doesn't get included in the property. I realize you can add custom JSON replacer functions (basically extending the JSON.stringify functionality), but I'm not a big fan of that because it means any time you serialized these tracked JavaScript Objects into JSON, you would have to know/remember to add that replacer function which is pretty impractical.

Is there any way to accomplish this?

回答1:

Well I cannot reproduce it in IE8 compatability mode in IE10 but defining a property like "toLocaleString" should work because of the don't enum bug in IE8.

var uniqueId = function() {
    var dontEnumBug = false;
    var id = 0;

    if( !Object.defineProperty ) {
        var keyVisited = false;
        for( var k in {toLocaleString: 3}) {
            if( k === "toLocaleString" ) {
                keyVisited = true; 
            }
        }
        if( !keyVisited ) {
            dontEnumBug = true;
        }
    }

    return function( obj ) {
        if( dontEnumBug ) {
            obj.toLocaleString = id++;
        }
        else {
            Object.defineProperty(obj, '__id__', { enumerable: false, value: id++ });
        }

    }

})();

You could also use "isPrototypeOf" or "propertyIsEnumerable" as these are also functions that are pretty much never called.