Skip to content Skip to sidebar Skip to footer

Implement Alternative To Object.seal, Object.freeze, Object.preventExtensions

I have a need for the following functionality. I want to freeze the existing properties of an object, but allow for new properties to be added. For some reason, there seems to be n

Solution 1:

You can easily implement the setIntegrityLevel algorithm without preventing extensions yourself:

Object.freezeExisting = function(o) {
    for (const key of Reflect.ownKeys(o)) {
        const desc = Object.getOwnPropertyDescriptor(o, key);
        if (!desc)
            continue;
        if ("value" in desc)
            Object.defineProperty(o, key, {configurable: false, writable: false});
        else
            Object.defineProperty(o, key, {configurable: false});
    }
    return o;
};

Post a Comment for "Implement Alternative To Object.seal, Object.freeze, Object.preventExtensions"