CKTIS / JavaScript
Object.preventExtensions — blocking new properties
Object.preventExtensions(obj) irreversibly blocks adding own properties to an object and returns that same object. It does not freeze existing values or nested objects.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
Object.preventExtensions(obj)Object.preventExtensions(obj) makes an object non-extensible: new own properties cannot be added, including properties with symbol keys. The method mutates the supplied object and returns the same reference, not a copy. Use Object.isExtensible(obj) to check its state. The operation cannot be reversed.
For an ordinary object, existing properties retain their descriptors: writable properties can still be assigned new values, and configurable properties can still be deleted. A deleted property cannot be added back. Changing the prototype to a different one is also prohibited; specifying the existing prototype remains allowed.
A fixed-length typed array backed by a non-resizable ArrayBuffer can be made non-extensible, but its existing indexed elements cannot be deleted even though their descriptors report configurable: true.
An assignment that would create a new own property throws TypeError in strict mode and is usually ignored outside strict mode. Object.defineProperty throws TypeError when asked to add a property regardless of mode. The method itself also throws TypeError if the internal operation refuses prevention, for example through a Proxy trap returning false. In current ECMAScript, primitive arguments, including null and undefined, are returned unchanged.
Object.seal provides stronger protection by setting configurable to false for all own properties, so they cannot be deleted. On a sealed ordinary object, a writable data property's writable attribute can still be changed from true to false. A sealed module namespace object reports writable: true for the properties corresponding to its exported bindings but rejects changing that attribute to false. On an ordinary object, Object.freeze additionally sets writable to false for all own data properties. On a module namespace object, Object.freeze throws TypeError when it attempts to set writable to false for a property corresponding to an exported binding. If that binding is still uninitialized, looking up the property's descriptor can throw ReferenceError before that attempt. None of these operations recursively affects nested objects. This is an ECMAScript language API, not a browser Web API; the standard alone does not establish support in a particular browser.
Examples
"use strict";
const item = { count: 1, temporary: true };
const result = Object.preventExtensions(item);
console.log(result === item);
console.log(Object.isExtensible(item));
item.count = 2;
console.log(item.count);
console.log(delete item.temporary);
try {
item.extra = 3;
} catch (error) {
console.log(error.name);
}
console.log(Object.hasOwn(item, "extra"));Result
true false 2 true TypeError false
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.