JS: Reflect.preventExtensions
New in JS2015.
Reflect.preventExtensions(obj)
-
- Make object not extensible.
- Return
true
if success, elsefalse
. - If obj is not object, throw a TypeError exception.
const jj = {}; console.log(Reflect.isExtensible(jj)); Reflect.preventExtensions(jj); console.log(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 = { "p": 3 }; Reflect.preventExtensions(jj); console.log(jj.hasOwnProperty("p")); delete jj.p; console.log(jj.hasOwnProperty("p") === false);
〔see Property Attributes〕