Table of Contents
Object.preventExtensions() prevents new properties from being added to an object. Existing properties can still be changed or deleted:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object Protection</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Object.preventExtensions()</h4>
<p id="demo"></p>
<script>
const person = {
firstName: "John",
lastName: "Doe",
age: 30
};
// Prevent adding new properties
Object.preventExtensions(person);
// Try to add a new property
person.nationality = "American";
document.getElementById("demo").innerHTML =
"firstName: " + person.firstName + "<br>" +
"nationality: " + person.nationality + " (undefined - cannot add)";
</script>
</body>
</html>

Result View Example
Object.isExtensible() checks if new properties can be added to an object:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object Protection</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Object.isExtensible()</h4>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
const person1 = { firstName: "John" };
const person2 = { firstName: "Jane" };
Object.preventExtensions(person2);
document.getElementById("demo1").innerHTML =
"person1 is extensible: " + Object.isExtensible(person1);
document.getElementById("demo2").innerHTML =
"person2 is extensible: " + Object.isExtensible(person2);
</script>
</body>
</html>

Result View Example
Object.seal() prevents adding or deleting properties. Existing properties can still be changed:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object Protection</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Object.seal()</h4>
<p id="demo"></p>
<script>
const person = {
firstName: "John",
lastName: "Doe",
age: 30
};
// Seal the object
Object.seal(person);
// Try to add a new property (will be ignored in non-strict mode)
person.nationality = "American";
// Try to delete a property (will be ignored in non-strict mode)
delete person.age;
// Change existing property (allowed)
person.firstName = "Jane";
document.getElementById("demo").innerHTML =
"firstName: " + person.firstName + "<br>" +
"age: " + person.age + " (still exists, cannot delete)" + "<br>" +
"nationality: " + person.nationality + " (cannot add)";
</script>
</body>
</html>

Result View Example
Object.isSealed() checks if an object is sealed:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object Protection</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Object.isSealed()</h4>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
const person1 = { firstName: "John" };
const person2 = { firstName: "Jane" };
Object.seal(person2);
document.getElementById("demo1").innerHTML =
"person1 is sealed: " + Object.isSealed(person1);
document.getElementById("demo2").innerHTML =
"person2 is sealed: " + Object.isSealed(person2);
</script>
</body>
</html>

Result View Example
Object.freeze() prevents any changes to an object. No properties can be added, deleted, or changed:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object Protection</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Object.freeze()</h4>
<p id="demo"></p>
<script>
const person = {
firstName: "John",
lastName: "Doe",
age: 30
};
// Freeze the object
Object.freeze(person);
// Try to change a property (will be ignored in non-strict mode)
person.firstName = "Jane";
// Try to add a new property
person.nationality = "American";
// Try to delete a property
delete person.age;
document.getElementById("demo").innerHTML =
"firstName: " + person.firstName + " (cannot change)" + "<br>" +
"age: " + person.age + " (cannot delete)" + "<br>" +
"nationality: " + person.nationality + " (cannot add)";
</script>
</body>
</html>

Result View Example
Object.isFrozen() checks if an object is frozen:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object Protection</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Object.isFrozen()</h4>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
const person1 = { firstName: "John" };
const person2 = { firstName: "Jane" };
Object.freeze(person2);
document.getElementById("demo1").innerHTML =
"person1 is frozen: " + Object.isFrozen(person1);
document.getElementById("demo2").innerHTML =
"person2 is frozen: " + Object.isFrozen(person2);
</script>
</body>
</html>

Result View Example
Document in project
You can Download PDF file.