revision:
The Object.isFrozen() static method determines if an object is frozen.
Object.isFrozen(obj)
Parameters:
obj : required. The object which should be checked.
const object1 = { property1: 42 }; console.log(Object.isFrozen(object1)); // Expected output: false Object.freeze(object1); console.log(Object.isFrozen(object1)); // Expected output: true
<div> <p id="frozen-1"></p> <p id="frozen-2"></p> <p id="frozen-3"></p> <p id="frozen-4"></p> <p id="frozen-5"></p> <p id="frozen-6"></p> <p id="frozen-7"></p> <p id="frozen-8"></p> <p id="frozen-9"></p> <p id="frozen-10"></p> <p id="frozen-11"></p> <p id="frozen-12"></p> </div> <script> // A new object is extensible, so it is not frozen. Object.isFrozen({}); // false document.getElementById("frozen-1").innerHTML = "is frozen ? : " + Object.isFrozen({}); // An empty object which is not extensible is vacuously frozen. const vacuouslyFrozen = Object.preventExtensions({}); Object.isFrozen(vacuouslyFrozen); // true document.getElementById("frozen-2").innerHTML = "is frozen ? : " + Object.isFrozen(vacuouslyFrozen); // A new object with one property is also extensible, ergo not frozen. const oneProp = { p: 42 }; Object.isFrozen(oneProp); // false document.getElementById("frozen-3").innerHTML = "is frozen ? : " + Object.isFrozen(oneProp); // Preventing extensions to the object still doesn't make it frozen, because the property is still configurable (and writable). Object.preventExtensions(oneProp); Object.isFrozen(oneProp); // false document.getElementById("frozen-3").innerHTML = "is frozen ? : " + Object.isFrozen(oneProp); // Deleting that property makes the object vacuously frozen. delete oneProp.p; Object.isFrozen(oneProp); // true document.getElementById("frozen-4").innerHTML = "is frozen ? : " + Object.isFrozen(oneProp); // A non-extensible object with a non-writable but still configurable property is not frozen. const nonWritable = { e: "plep" }; Object.preventExtensions(nonWritable); Object.defineProperty(nonWritable, "e", { writable: false, }); // make non-writable Object.isFrozen(nonWritable); // false document.getElementById("frozen-5").innerHTML = "is frozen ? : " + Object.isFrozen(nonWritable); // Changing that property to non-configurable then makes the object frozen. Object.defineProperty(nonWritable, "e", { configurable: false, }); // make non-configurable Object.isFrozen(nonWritable); // true document.getElementById("frozen-6").innerHTML = "is frozen ? : " + Object.isFrozen(nonWritable); // A non-extensible object with a non-configurable but still writable property also isn't frozen. const nonConfigurable = { release: "the kraken!" }; Object.preventExtensions(nonConfigurable); Object.defineProperty(nonConfigurable, "release", { configurable: false, }); Object.isFrozen(nonConfigurable); // false document.getElementById("frozen-7").innerHTML = "is frozen ? : " + Object.isFrozen(nonConfigurable); // Changing that property to non-writable then makes the object frozen. Object.defineProperty(nonConfigurable, "release", { writable: false, }); Object.isFrozen(nonConfigurable); // true document.getElementById("frozen-8").innerHTML = "is frozen ? : " + Object.isFrozen(nonConfigurable); // A non-extensible object with a configurable accessor property isn't frozen. const accessor = { get food() { return "yum"; }, }; Object.preventExtensions(accessor); Object.isFrozen(accessor); // false document.getElementById("frozen-9").innerHTML = "is frozen ? : " + Object.isFrozen(accessor); // When we make that property non-configurable it becomes frozen. Object.defineProperty(accessor, "food", { configurable: false, }); Object.isFrozen(accessor); // true document.getElementById("frozen-10").innerHTML = "is frozen ? : " + Object.isFrozen(accessor); // But the easiest way for an object to be frozen is if Object.freeze has been called on it. const frozen = { 1: 81 }; Object.isFrozen(frozen); // false Object.freeze(frozen); Object.isFrozen(frozen); // true document.getElementById("frozen-11").innerHTML = "is frozen ? : " + Object.isFrozen(frozen); // By definition, a frozen object is non-extensible. Object.isExtensible(frozen); // false // Also by definition, a frozen object is sealed. Object.isSealed(frozen); // true document.getElementById("frozen-12").innerHTML = "is frozen ? : " + Object.isSealed(frozen); </script>