JavaScript Proxy handler.preventExtensions() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Proxy trap

Definition

handler.preventExtensions(target) is a trap that runs when someone tries to lock a Proxy against new properties. This happens with Object.preventExtensions() and Reflect.preventExtensions(), and also with Object.seal() and Object.freeze(), which start by doing the same thing.

The trap returns true when the object is locked and false when it is not. To lock the target in the normal way, use Reflect.preventExtensions(target).

Remember
const handler = {
  preventExtensions(target) { ... }
};

target  → the original object

return true  → only if the target is now locked
return false → refuse (Object.preventExtensions throws)

Locking the target is YOUR job inside the trap

Syntax

Add a preventExtensions trap to the handler of a Proxy.

JavaScript
const proxy = new Proxy({ a: 1 }, {
  preventExtensions(target) {
    console.log("Locking");
    return Reflect.preventExtensions(target);
  }
});

Object.preventExtensions(proxy);   // Locking
console.log(Object.isExtensible(proxy));   // false
CallWhat it does
Object.preventExtensions(proxy)Asks to lock the object. Runs the trap.
Reflect.preventExtensions(proxy)The function-style version. It returns true or false.
Object.seal(proxy), Object.freeze(proxy)Both lock the object first, so they run the trap.
Object.isExtensible(proxy)Does not run this trap. It uses isExtensible.

A standard JavaScript Proxy trap (ES2015). You write it inside the handler object that you pass to new Proxy(). JavaScript calls it for you. No import required. Siblings: Proxy.revocable(), handler.apply(), handler.construct(), handler.defineProperty(), handler.deleteProperty(), handler.get(), handler.getOwnPropertyDescriptor(), handler.getPrototypeOf(), handler.has(), handler.isExtensible(), handler.ownKeys(), handler.set(), handler.setPrototypeOf().

Library declarations
JavaScript
// ECMAScript 2015 (ES6)
handler.preventExtensions(target)

// target  : the original object
// returns : true or false

Parameters

ParameterDescription
targetThe original object that the Proxy wraps.

Return Value

Return ValueDescription
trueThe target is now locked. This is only allowed when Object.isExtensible(target) is false.
falseThe lock is refused. Reflect.preventExtensions() returns false and Object.preventExtensions() throws a TypeError.
TypeError (thrown)The trap returned true, but the target can still be extended.

JavaScript Examples

Example 1 — Log Every Lock Request

Write a note each time someone locks the Proxy, and then lock the target for real. Object.seal() runs the trap too.

JavaScript
const logs = [];
const proxy = new Proxy({ a: 1 }, {
  preventExtensions(target) {
    logs.push("lock requested");
    return Reflect.preventExtensions(target);
  }
});

console.log("Before: " + Object.isExtensible(proxy));
Object.preventExtensions(proxy);
console.log("After: " + Object.isExtensible(proxy));
console.log("Log size: " + logs.length);

Object.seal(proxy);
console.log("Log size after seal: " + logs.length);
Try It Yourself

How It Works

  1. Lock for real. The trap saves a note and returns Reflect.preventExtensions(target), which locks the target and returns true.
  2. The Proxy follows the target. After the lock, Object.isExtensible(proxy) is false.
  3. Seal asks again. Object.seal() starts by locking, so the trap runs a second time. Locking twice is harmless.

Example 2 — Refuse to Be Locked

Return false to keep an object open. See how Reflect, Object.preventExtensions() and Object.freeze() react. The catch blocks print the error type only.

JavaScript
const open = new Proxy({ a: 1 }, {
  preventExtensions() {
    return false;
  }
});

console.log("Reflect: " + Reflect.preventExtensions(open));

try {
  Object.preventExtensions(open);
} catch (err) {
  console.log("Object.preventExtensions: " + err.name);
}

try {
  Object.freeze(open);
} catch (err) {
  console.log("Object.freeze: " + err.name);
}

open.b = 2;
console.log("Still extensible: " + Object.isExtensible(open));
console.log("Keys: " + Object.keys(open).join(", "));
Try It Yourself

How It Works

  1. A refusal. The trap always returns false. Reflect.preventExtensions() shows it as false.
  2. Louder methods. Object.preventExtensions() and Object.freeze() turn the refusal into a TypeError.
  3. Still open. Nothing was locked, so the object can still grow. Adding b works, and both keys are listed.

Example 3 — A Stronger Lock and the Rules

The trap can lock the target harder than asked. Then see that returning true without locking is caught. The catch block prints the error type only.

JavaScript
const data = { x: 1 };
const strong = new Proxy(data, {
  preventExtensions(target) {
    Object.freeze(target);
    return true;
  }
});

Object.preventExtensions(strong);
console.log("Extensible: " + Object.isExtensible(data));
console.log("Frozen: " + Object.isFrozen(data));

const liar = new Proxy({}, {
  preventExtensions() {
    return true;
  }
});
try {
  Object.preventExtensions(liar);
} catch (err) {
  console.log("True without locking: " + err.name);
}
Try It Yourself

How It Works

  1. Lock harder. The trap calls Object.freeze(target), which also makes the target non-extensible. So returning true is correct.
  2. More than asked. The caller only asked to prevent extensions, but the data is now frozen, as the last check shows.
  3. No empty promises. The second trap says true but locks nothing. JavaScript sees that the target is still extensible and throws a TypeError.

Browser & Runtime Support

handler.preventExtensions() is part of ES2015 (ES6), together with the Proxy object and the Reflect object. Every current browser and Node.js has it.

✓ ES2015 · Widely supported

handler.preventExtensions()

Supported in Chrome 49+, Edge 12+, Firefox 49+, Safari 10+, Opera 36+ and Node.js 6+. Internet Explorer never got it.

ES2015 Standard since
Google Chrome 49+ · Desktop & Mobile
Full support
Microsoft Edge 12+ · Chromium and legacy
Full support
Mozilla Firefox 49+ · Desktop & Mobile
Full support
Apple Safari 10+ · macOS & iOS
Full support
Opera 36+ · Chromium based
Full support
Samsung Internet Recent versions · Android
Supported
Android WebView 49+ · Chromium based
Supported
Node.js 6+ · Server runtime
Supported
Deno Supported · JavaScript runtime
Supported
Bun Supported · JavaScript runtime
Supported
Internet Explorer No native support
Not supported
handler.preventExtensions() Excellent

Bottom line: Safe to use in every current browser and in Node.js. A Proxy cannot be faked in old browsers, so do not use it where Internet Explorer must work.

Frequently Asked Questions

It is a method of the handler object that runs when someone locks a Proxy against new properties, with Object.preventExtensions(), Reflect.preventExtensions(), Object.seal() or Object.freeze(). It returns true or false.
Yes. If you return true while the target can still be extended, JavaScript throws a TypeError. Call Reflect.preventExtensions(target) inside the trap and return its result.
Return false. Reflect.preventExtensions() then returns false, while Object.preventExtensions(), Object.seal() and Object.freeze() throw a TypeError.
Yes. Freezing first prevents extensions, which runs this trap, and then it changes each property with the defineProperty trap. If the first step is refused, freeze() throws a TypeError.
preventExtensions is for the action of locking. isExtensible is for the question of whether an object is locked. The first changes the target, and the second only reports the state.

Next: handler.set()

Learn how a Proxy can check or block every property change with the set trap.

handler.set() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful