revision:
The Object.preventExtensions() static method prevents new properties from ever being added to an object (i.e. prevents future extensions to the object). It also prevents the object's prototype from being re-assigned.
Object.preventExtensions(obj)
Parameters:
obj : required. The object which should be made non-extensible.
const object1 = {}; Object.preventExtensions(object1); try { Object.defineProperty(object1, 'property1', { value: 42 }); } catch (e) { console.log(e); // Expected output: TypeError: Cannot define property property1, object is not extensible }
<div> <p id="prevent-1"></p> <p id="prevent-2"></p> <p id="prevent-3"></p> <p id="prevent-4"></p> <p id="prevent-5"></p> <p id="prevent-6"></p> </div> <script> // Object.preventExtensions returns the object being made non-extensible. const obj = {}; const obj2 = Object.preventExtensions(obj); obj === obj2; // true document.getElementById("prevent-1").innerHTML = " prevented extensions ? : " + (obj === obj2); // Objects are extensible by default. const empty = {}; Object.isExtensible(empty); // true document.getElementById("prevent-2").innerHTML = " is extensible ? : " + Object.isExtensible(empty); // They can be made un-extensible Object.preventExtensions(empty); Object.isExtensible(empty); // false document.getElementById("prevent-3").innerHTML = " is extensible ? : " + Object.isExtensible(empty); // Object.defineProperty throws when adding a new property to a non-extensible object. const nonExtensible = { removable: true }; Object.preventExtensions(nonExtensible); Object.defineProperty(nonExtensible, "new", { value: 8675309, }); // throws a TypeError // In strict mode, attempting to add new properties to a non-extensible object throws a TypeError. function fail() { "use strict"; // throws a TypeError nonExtensible.newProperty = "FAIL"; } fail(); </script>
<div> <p id="prevent-7"></p> </div> <script> let ninjas1 = {}; Object.preventExtensions(ninjas1); try { Object.defineProperty(ninjas1, 'prop1', { value: "NFN", property1: "ninjasforninjas", property2: "a good platform to learn" }); } catch (error) { console.log(error); document.getElementById("prevent-7").innerHTML = error; } </script>
<div> <p id="prevent-8"></p> <p id="prevent-9"></p> <p id="prevent-10"></p> <p id="prevent-11"></p> </div> <script> let ninjas = {}; let ninjas0 = Object.preventExtensions(ninjas); console.log(ninjas0 === ninjas); document.getElementById("prevent-8").innerHTML = " prevented extensions ? : " + (ninjas0 === ninjas); const ninjas2 = { "prop1": 555 }; Object.preventExtensions(ninjas2); delete ninjas2.prop1; console.log(ninjas2.hasOwnProperty("prop1")); document.getElementById("prevent-9").innerHTML = " has own property ? : " + (ninjas2.hasOwnProperty("prop1")); const ninjas3 = {}; Object.preventExtensions(ninjas3); ninjas3.prop2 = 3; console.log( ninjas3.hasOwnProperty("prop2") ); document.getElementById("prevent-10").innerHTML = " has own property ? : " + (ninjas3.hasOwnProperty("prop2")); const ninjas4 = {}; Object.preventExtensions(ninjas4); console.log( Object.isExtensible(ninjas4) ); document.getElementById("prevent-11").innerHTML = " is extensiblle ? : " + (Object.isExtensible(ninjas4)); </script>