How to add non-enumerable property in JavaScript for IE8?

我只是一个虾纸丫 提交于 2019-12-12 22:41:09

问题


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.



来源:https://stackoverflow.com/questions/17934888/how-to-add-non-enumerable-property-in-javascript-for-ie8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!