In strict mode, attempting to add new properties to a non-extensible object throws a TypeError
. In sloppy mode, the addition of the "x" property is silently ignored.
"use strict";
const obj = {};
Object.preventExtensions(obj);
obj.x = "foo";
In both, strict mode and sloppy mode, a call to Object.defineProperty()
throws when adding a new property to a non-extensible object.
const obj = {};
Object.preventExtensions(obj);
Object.defineProperty(obj, "x", { value: "foo" });
To fix this error, you will either need to remove the call to Object.preventExtensions()
entirely, or move it to a position so that the property is added earlier and only later the object is marked as non-extensible. Of course you can also remove the property that was attempted to be added, if you don't need it.
"use strict";
const obj = {};
obj.x = "foo";
Object.preventExtensions(obj);