Science Knowings: JavaScript Course For Social Media

Object.seal() Method

From Object.freeze() to Object.seal()

In our previous session, we learned about the Object.freeze() method. Continuing our exploration, we'll now dive into the Object.seal() method, another powerful tool for managing JavaScript objects.

What is Object.seal()?

Object.seal() is a method that makes an object read-only and prevents the addition of new properties. However, unlike Object.freeze(), it allows existing properties to be modified.

Difference between Object.freeze() and Object.seal()

Property ModificationDisallowedAllowed
Property Addition/DeletionDisallowedDisallowed
Object ExtensibilityFalseTrue

Use Cases and Benefits of Object.seal()

  • Protecting sensitive data from accidental changes while allowing updates
  • Enforcing object structure and preventing accidental property additions
  • Guaranteeing object integrity in multi-threaded environments
  • Improving performance by reducing the need for constant property checks
  • Browser Compatibility

    Object.seal() is supported in all major modern browsers:

    • Chrome
    • Firefox
    • Safari
    • Edge
    • Opera

    Polyfill for Older Browsers

    For older browsers that don't support Object.seal(), you can use the following polyfill:

    if (!Object.seal) {Object.seal = function(obj) {if (Object.isFrozen(obj)) {return obj;}Object.preventExtensions(obj);return obj;};}

    JavaScript Object Properties

    Every JavaScript object has three essential properties:

    • configurable: Specifies whether the property can be deleted or reconfigured
    • writable: Specifies whether the property's value can be changed
    • enumerable: Specifies whether the property is included in certain operations like Object.keys()

    What is Object Sealing?

    Object sealing involves setting the configurable property of all an object's properties to false, effectively preventing the addition or deletion of properties.

    How Object.seal() Works

    Object.seal() sets the configurable property of all an object's properties to false. This prevents the addition or deletion of properties but does not affect the writable property, allowing existing properties to be modified.

    Preventing Property Addition or Deletion

    After sealing an object, attempting to add a new property will result in an error. Similarly, trying to delete an existing property will fail.

    What Properties Can Be Sealed?

    Object.seal() can seal both data properties (which contain a value) and accessor properties (getters and setters).

    Sealed vs. Frozen Objects

    Sealed objects are similar to frozen objects, but with one key difference: sealed objects allow property values to be modified while frozen objects do not. This makes sealing suitable for scenarios where you want to prevent structural changes but allow data updates.

    When to Use Object.seal()

  • When you want to protect an object from accidental structural changes
  • When you need to prevent property additions or deletions
  • When you want to enforce object integrity in multi-threaded environments
  • When you want to improve performance by reducing the need for constant property checks
  • When Not to Use Object.seal()

  • When you need to add or delete properties dynamically
  • When you require complete immutability (use Object.freeze() instead)
  • Limitations of Object.seal()

  • Sealed objects can still be modified through their non-configurable property descriptors (e.g., Object.defineProperty())
  • Sealing objects does not prevent accidental modifications to nested objects
  • Next Topic: Object.assign() Method

    In our next session, we'll explore the Object.assign() method, which allows you to copy properties from one or more source objects to a target object. Follow us to learn more!