|
| 1 | +--- |
| 2 | +Title: '.seal()' |
| 3 | +Description: 'Prevents new properties from being added to an object and marks all existing properties as non-configurable' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Web Development' |
| 7 | +Tags: |
| 8 | + - 'Methods' |
| 9 | + - 'Objects' |
| 10 | + - 'Properties' |
| 11 | +CatalogContent: |
| 12 | + - 'introduction-to-javascript' |
| 13 | + - 'paths/front-end-engineer-career-path' |
| 14 | +--- |
| 15 | + |
| 16 | +The **`Object.seal()`** static method seals an object, preventing new properties from being added and making all existing properties non-configurable. In other words: |
| 17 | + |
| 18 | +- New properties cannot be added. |
| 19 | +- Existing properties cannot be removed. |
| 20 | +- Property enumerability and configurability cannot be changed. |
| 21 | + |
| 22 | +Unlike [`Object.freeze()`](https://www.codecademy.com/resources/docs/javascript/objects/freeze), objects sealed with `Object.seal()` may still have their existing properties updated if they are writable (`writable: true`). |
| 23 | + |
| 24 | +> **Note:** For debugging purposes, it is good practice to use [`strict mode`](https://www.codecademy.com/resources/docs/javascript/strict-mode) when working with sealed objects, as it helps detect unauthorized modifications and prevents silent errors. |
| 25 | +
|
| 26 | +## Syntax |
| 27 | + |
| 28 | +```pseudo |
| 29 | +Object.seal(obj) |
| 30 | +``` |
| 31 | + |
| 32 | +**Parameters:** |
| 33 | + |
| 34 | +`obj`: The object to seal. |
| 35 | + |
| 36 | +**Return value:** |
| 37 | + |
| 38 | +The `Object.seal()` method returns the sealed object. |
| 39 | + |
| 40 | +## Example 1: Basic Object Sealing |
| 41 | + |
| 42 | +In this example, the `person` object is sealed. In non-strict mode, attempts to add or remove properties will fail silently (no error is thrown), while modifying writable properties will still work: |
| 43 | + |
| 44 | +```js |
| 45 | +const person = { |
| 46 | + name: 'Joe', |
| 47 | + age: 30, |
| 48 | +}; |
| 49 | + |
| 50 | +// Sealing the person object |
| 51 | +Object.seal(person); |
| 52 | + |
| 53 | +// Modifying the age property will work |
| 54 | +person.age = 31; |
| 55 | + |
| 56 | +// Attempting to add or delete a property will not work |
| 57 | +person.city = 'Madrid'; |
| 58 | +delete person.name; |
| 59 | + |
| 60 | +console.log(person); |
| 61 | +``` |
| 62 | + |
| 63 | +The code will produce this output: |
| 64 | + |
| 65 | +```shell |
| 66 | +{ name: 'Joe', age: 31 } |
| 67 | +``` |
| 68 | +
|
| 69 | +## Example 2: Check Sealed Object |
| 70 | +
|
| 71 | +The following example uses the [`Object.isSealed()`](https://www.codecademy.com/resources/docs/javascript/objects/isSealed) to check if the `car` object is sealed. This method returns `true` if the object is sealed, regardless of how it was sealed (e.g., via `Object.seal()` or `Object.freeze()`: |
| 72 | +
|
| 73 | +```js |
| 74 | +const car = { |
| 75 | + brand: 'Audi', |
| 76 | + model: 'Sedan', |
| 77 | +}; |
| 78 | +
|
| 79 | +// Sealing the car object |
| 80 | +Object.seal(car); |
| 81 | +
|
| 82 | +// This will print 'true' because the car object is sealed |
| 83 | +console.log(Object.isSealed(car)); |
| 84 | +
|
| 85 | +car.brand = 'Honda'; |
| 86 | +car.color = 'red'; |
| 87 | +
|
| 88 | +console.log(car); |
| 89 | +``` |
| 90 | +
|
| 91 | +The code will produce this output: |
| 92 | +
|
| 93 | +```shell |
| 94 | +true |
| 95 | +{ brand: 'Honda', model: 'Sedan' } |
| 96 | +``` |
| 97 | +
|
| 98 | +## Codebyte Example: Strict Mode Behavior |
| 99 | +
|
| 100 | +This example demonstrates the core functionality of `Object.seal()` by showing what operations are allowed versus prohibited on a sealed object. The sealed object allows modification of existing properties but prevents structural changes like adding new properties or deleting existing ones: |
| 101 | +
|
| 102 | +```codebyte/javascript |
| 103 | +'use strict'; |
| 104 | +
|
| 105 | +const obj = { name: 'John', age: 25 }; |
| 106 | +
|
| 107 | +Object.seal(obj); |
| 108 | +
|
| 109 | +obj.age = 26; |
| 110 | +console.log(obj.age); // 26 |
| 111 | +
|
| 112 | +try { |
| 113 | + obj.city = 'NYC'; // Can't add new properties |
| 114 | +} catch (e) { |
| 115 | + console.log('Cannot add property:', e.message); |
| 116 | +} |
| 117 | +
|
| 118 | +try { |
| 119 | + delete obj.name; // Can't delete existing properties |
| 120 | +} catch (e) { |
| 121 | + console.log('Cannot delete property:', e.message); |
| 122 | +} |
| 123 | +``` |
0 commit comments