javascriptdefineproperty

Is there any way to delete a property that is read-only and non-configurable?


I think the answer is "no", other than deleting all references to the containing object and allowing garbage collection to eventually delete all contents of the containing object.

Live Example (view log output with console (hit F12 in Chrome, etc))

Code:

(function () {

    var nameValue = "uninitialized";

    Object.defineProperty(this, "name", {
        enumerable: true,
        configurable: false,
        get: function () {
            return nameValue;
        },
        set: function () {
            console.log("This is a read-only property");
        }
    });
    console.log(nameValue);
    nameValue = "George";
    delete this.name;
    this.name = "";
    console.log(this.name);
})();

Solution

  • configurable true if and only if the type of this property descriptor may be changed and if the property may be deleted from the corresponding object. Defaults to false.

    Object.defineProperty on MDN

    So I would agree with you that it cannot be done.

    As you mention you could delete the whole object and if you first copy all the configurable properties you will, in effect have deleted them. If you do do this be aware that any other references to the original object will not be affected.