Object.seal()

ES5+

Seals an object, preventing new properties from being added and marking all existing properties as non-configurable.

Syntax

Object.seal(obj)

Parameters

obj Object

The object to seal

Return Value

Object

The sealed object

Examples

JavaScript
const obj = { name: 'John' };
Object.seal(obj);
obj.name = 'Jane'; // 가능
obj.age = 30; // 무시됨
console.log(obj);
Output:
// { name: 'Jane' }

📌 When to Use

Use Object.seal() when you want to prevent adding or removing properties but still allow modification of existing property values. Ideal for objects with a fixed schema where values may need updates, like user session objects or cached data structures.

⚠️ Common Mistakes

Confusing seal with freeze - sealed objects CAN have their property values changed, frozen objects cannot.

Expecting Object.seal() to be deep - like freeze, it only affects the top-level object, not nested objects.

Not knowing that deletion attempts fail silently in non-strict mode - use strict mode to catch these errors.

✅ Best Practices

Use Object.seal() for data transfer objects (DTOs) where the structure is fixed but values may be updated.

Combine with TypeScript interfaces for compile-time and runtime property protection.

Use Object.seal() over Object.freeze() when you need a mutable cache with a fixed key set.

⚡ Performance Notes

Object.seal() has similar performance characteristics to Object.freeze() - O(n) for n properties. Sealed objects may have slightly better performance than frozen objects in some scenarios since the engine knows values can still change, allowing certain optimizations.

🌍 Real World Example

User Session Manager with Fixed Properties

Create a user session object with a fixed schema where properties cannot be added or removed, but values can be updated.

function createSession(userId) {
  const session = {
    userId,
    token: null,
    expiresAt: null,
    lastActivity: Date.now(),
    isAuthenticated: false
  };

  // Seal the session - no new properties, but values can change
  return Object.seal(session);
}

const session = createSession('user123');

// Updating values works fine
session.token = 'abc123xyz';
session.isAuthenticated = true;
session.expiresAt = Date.now() + 3600000;
console.log(session.isAuthenticated); // true

// Adding new properties fails silently (or throws in strict mode)
session.newProperty = 'test';
console.log(session.newProperty); // undefined

// Deleting properties also fails
delete session.userId;
console.log(session.userId); // 'user123' (still exists)

// Check status
console.log(Object.isSealed(session)); // true
console.log(Object.isFrozen(session)); // false (values can still change)

Related Methods