JS: Reflect.preventExtensions
(new in ECMAScript 2015)
Reflect.preventExtensions(obj)-
- Make object not extensible.
- Return
trueif success, elsefalse. - If obj is not object, throw a TypeError exception.
const jj = {}; console.assert(Reflect.isExtensible(jj)); Reflect.preventExtensions(jj); console.assert(Reflect.isExtensible(jj) === false);
Non-extensible object, cannot revert, property can still be deleted, parent object may add properties
- Once a object is not extensible, you cannot revert it.
- Property can still be deleted for Non-Extensible object
- if a object is not extensible, but its parents may be, so you can add properties to the parent object, and your object may still get unexpected properties, because of inheritance.
// property can still be deleted for non-extensible object const jj = { "pp": 3 }; Reflect.preventExtensions(jj); console.assert(Object.hasOwn(jj, "pp") === true); Reflect.deleteProperty(jj, "pp"); console.assert(Object.hasOwn(jj, "pp") === false);
〔see Property Attributes〕
Reflect.preventExtensions does not change property descriptor.
/* show Reflect.preventExtensions() does not change property descriptor. */ const jj = { age: 33 }; console.assert(Reflect.isExtensible(jj) === true); const xdesc = Reflect.getOwnPropertyDescriptor(jj, "age"); console.assert(xdesc.writable === true); console.assert(xdesc.configurable === true); Reflect.preventExtensions(jj); console.assert(Reflect.isExtensible(jj) === false); console.assert(xdesc.writable === true); console.assert(xdesc.configurable === true);