JavaScript Basics: Object.seal and Object.isSealed

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@ghasemkiani·
0.000 HBD
JavaScript Basics: Object.seal and Object.isSealed
In the previous post, I wrote about `Object.preventExtensions`. That functions prevents the addition of new properties to an object. However, it does not prevent you from configuring, mutating, or even deleting the existing properties.

The following code snippet demonstrates the workings of `Object.preventExtensions`:

```
	let myObj = {
		name: "Ali",
	};
	Object.preventExtensions(myObj);
	// You can still configure the existing properties
	Object.defineProperty(myObj, "name", {
		get() {
			return "Susan";
		},
	});
	console.log(myObj.name); // Susan
	// You can even delete the existing properties
	delete myObj.name;
	console.log(myObj.name); // undefined
	
	// You just can't add new properties.
	// The following code throws an exception:
	// TypeError: Cannot define property age, object is not extensible
	Object.defineProperty(myObj, "age", {
		value: 19,
	});
```

_What if we want to prevent the existing properties from being configured again?_

For this purpose, you can use `Object.seal`. This function prevents adding new properties **and** prevents the existing properties from being reconfigured or deleted. After calling this function, the only thing you can do is to change the values of the existing properties. You cannot change their definition. For example, you cannot convert a data property into an accessor property or vice versa.

Here is an example:

```
	let myObj = {
		name: "Ali",
	};
	Object.seal(myObj);
	
	console.log(Object.isSealed(myObj)); // true

	// You cannot add new properties. This fails silently.
	myObj.age = 19;
	console.log(myObj.age); // undefined

	// You cannot reconfigure existing properties.
	try {
		Object.defineProperty(myObj, "name", {
			get() {
				return "Susan";
			},
		});
		console.log(myObj.name);
	} catch (e) {
		console.log(e); // TypeError: Cannot redefine property: name
	}

	// You can still change property values.
	myObj.name = "Susan";
	console.log(myObj.name); // Susan
	// This is also okay (since this only changes the value of the property
	// and does not change its configuration):
	Object.defineProperty(myObj, "name", {
		value: "Aria",
	});
	console.log(myObj.name); // Aria

	// You can add new properties to the prototype.
	myObj.__proto__.age = 14;
	console.log(myObj.age); // 14

	// You cannot change the prototype.
	try {
		Object.setPrototypeOf(myObj, {
			grade: 9,
		});
		console.log(myObj.grade);
	} catch (e) {
		console.log(e); // TypeError: #<Object> is not extensible
	}
	try {
		myObj.__proto__ = {
			grade: 9,
		};
		console.log(myObj.grade);
	} catch (e) {
		console.log(e); // TypeError: #<Object> is not extensible
	}
```

In short, `Object.seal` prevents extensions to the object and makes all the existing properties non-configurable, but does not prevent the values of the existing properties from being changed.

---

## Related Posts

* [JavaScript Basics: Object.create](https://steemit.com/javascript/@ghasemkiani/javascript-basics-01)
* [JavaScript Basics: Object.assign](https://steemit.com/javascript/@ghasemkiani/javascript-basics-02)
* [JavaScript Basics: Object.getPrototypeOf and Object.setPrototypeOf](https://steemit.com/javascript/@ghasemkiani/javascript-basics-03)
* [JavaScript Basics: Object.keys, Object.values, and Object.entries](https://steemit.com/javascript/@ghasemkiani/javascript-basics-04)
* [JavaScript Basics: Object.is](https://steemit.com/javascript/@ghasemkiani/javascript-basics-05)
* [JavaScript Basics: Object.prototype.hasOwnProperty](https://steemit.com/javascript/@ghasemkiani/javascript-basics-06)
* [JavaScript Basics: Object.preventExtensions and Object.isExtensible](https://steemit.com/javascript/@ghasemkiani/javascript-basics-07)
👍 , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,