Skip to content Skip to sidebar Skip to footer

How To Add Non-enumerable Property In Javascript For Ie8?

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__',

Solution 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;
        }
    }

    returnfunction( 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.

Post a Comment for "How To Add Non-enumerable Property In Javascript For Ie8?"