JavaScript Proxy defineProperty Trap Explained With Examples
The Proxy defineProperty trap intercepts Object.defineProperty, freeze and some assignments. See blocked accessors, the set trap link and the invariants.

Object.defineProperty() is how code creates a property with specific flags: read-only, hidden from enumeration, locked against deletion. A Proxy with a defineProperty trap sees those calls, so it can refuse them, rewrite their flags, or record them. That alone would make it a niche tool. What makes it matter is that it is also called in places you may not expect: Object.freeze() goes through it, and so do ordinary assignments when the set trap forwards the receiver. A proxy that blocks defineProperty can break writes that its set trap allowed, and one that does not handle it can be frozen by someone else's code. This post covers the signature, the descriptor the trap receives, forcing flags, blocking accessors, how freeze and assignments reach the trap, the interaction with set, and the invariants. The examples assume Node.js 24 in an ES module.
The signature and the descriptor
const proxy = new Proxy({}, {
defineProperty(target, property, descriptor) {
console.log("define", String(property), JSON.stringify(descriptor));
return Reflect.defineProperty(target, property, descriptor);
},
});
Object.defineProperty(proxy, "id", { value: 1 });
Object.defineProperty(proxy, "name", { value: "Ada", enumerable: true, writable: true });
console.log(Object.getOwnPropertyDescriptor(proxy, "id"));
console.log(Object.keys(proxy));define id {"value":1}
define name {"value":"Ada","writable":true,"enumerable":true}
{ value: 1, writable: false, enumerable: false, configurable: false }
[ 'name' ]The trap receives the target, the key and a descriptor object that contains only the fields the caller supplied. The first call set just value, so the trap saw {"value":1}, and the defaults, all false, were filled in by the target when Reflect.defineProperty() created the property. The trap returns a boolean saying whether the definition succeeded. It runs for Object.defineProperty(), Object.defineProperties() and Reflect.defineProperty().
Refusing a definition
Returning false makes Object.defineProperty() throw, and Reflect.defineProperty() return false.
const dataOnly = new Proxy({}, {
defineProperty(target, property, descriptor) {
if ("get" in descriptor || "set" in descriptor) return false;
return Reflect.defineProperty(target, property, descriptor);
},
});
Object.defineProperty(dataOnly, "plain", { value: 1, enumerable: true });
try {
Object.defineProperty(dataOnly, "computed", { get: () => 1 });
} catch (e) {
console.log(e.name);
}
console.log(Reflect.defineProperty(dataOnly, "other", { set() {} }));
console.log(Object.keys(dataOnly));TypeError
false
[ 'plain' ]The first definition is allowed and the accessor is refused, with a TypeError from Object.defineProperty() and a plain false from Reflect.defineProperty(). This is a way to keep an object made only of plain data, which serializes and copies predictably, since getters and setters run code on every read. The check uses in on the descriptor because a descriptor with get: undefined still counts as an accessor.
Forcing flags
The trap can change the descriptor before passing it on.
const visible = new Proxy({}, {
defineProperty(target, property, descriptor) {
return Reflect.defineProperty(target, property, {
...descriptor,
enumerable: true,
configurable: true,
});
},
});
Object.defineProperty(visible, "a", { value: 1 });
Object.defineProperty(visible, "b", { value: 2, enumerable: false });
console.log(Object.keys(visible));
console.log(JSON.stringify(visible));[ 'a', 'b' ]
{"a":1,"b":2}The caller asked for b to be hidden and the trap overrode it, so both keys are enumerable and both appear in the JSON text. The trap also sets configurable: true, and that is not decoration: if the property were created non-configurable, the engine would compare it with the caller's enumerable: false, find a conflict that can never be resolved, and throw a TypeError. This is the kind of rule that is surprising when it is not documented: anyone who defines a property with enumerable: false on this object gets a different result from what they wrote. Use it for an object whose whole purpose is to be fully visible, such as a state container where hidden properties would defeat serialization.
Plain assignments can reach the trap
A property assignment does not call defineProperty directly, and it can still arrive there. When the proxy has no set trap, the assignment is carried out with the proxy as the receiver, and the engine creates or updates the property through the proxy's own defineProperty.
const seen = [];
const proxy = new Proxy({}, {
defineProperty(target, property, descriptor) {
seen.push(`${String(property)} ${JSON.stringify(descriptor)}`);
return Reflect.defineProperty(target, property, descriptor);
},
});
proxy.a = 1;
proxy.a = 2;
proxy.b = 3;
console.log(seen.join("\n"));
console.log(Object.keys(proxy));a {"value":1,"writable":true,"enumerable":true,"configurable":true}
a {"value":2}
b {"value":3,"writable":true,"enumerable":true,"configurable":true}
[ 'a', 'b' ]A new property is created with all four flags and the first value. An update to an existing property is a definition with only value, and that is why a defineProperty trap written to expect full descriptors breaks on assignments: descriptor.writable is undefined for the second line. A trap that checks flags has to treat missing fields as "unchanged".
Freeze goes through the trap
Object.freeze() is implemented as a series of definitions, so a proxy sees it.
const events = [];
const proxy = new Proxy({ a: 1, b: 2 }, {
preventExtensions(target) {
events.push("preventExtensions");
return Reflect.preventExtensions(target);
},
defineProperty(target, property, descriptor) {
events.push(`define ${String(property)} ${JSON.stringify(descriptor)}`);
return Reflect.defineProperty(target, property, descriptor);
},
});
Object.freeze(proxy);
console.log(events.join("\n"));
console.log(Object.isFrozen(proxy));preventExtensions
define a {"writable":false,"configurable":false}
define b {"writable":false,"configurable":false}
trueFreezing first stops new keys from being added, then redefines each existing key as read-only and non-configurable. A proxy that returns false from defineProperty cannot be frozen: Object.freeze() throws a TypeError. That is a feature if you want to guard against someone freezing a shared object, and a problem if a library freezes its inputs and you hand it your proxy.
The set trap and the receiver
When a set trap forwards the receiver to Reflect.set, the write ends up in defineProperty on the proxy as well.
const log = [];
const handler = {
defineProperty(target, property) {
log.push(`define ${String(property)}`);
return false;
},
};
const withReceiver = new Proxy({}, {
...handler,
set(target, property, value, receiver) {
return Reflect.set(target, property, value, receiver);
},
});
const withoutReceiver = new Proxy({}, {
...handler,
set(target, property, value) {
return Reflect.set(target, property, value);
},
});
try {
withReceiver.x = 1;
} catch (e) {
console.log("with receiver:", e.name);
}
withoutReceiver.y = 2;
console.log("without receiver:", withoutReceiver.y);
console.log(log);with receiver: TypeError
without receiver: 2
[ 'define x' ]With the receiver forwarded, the write was carried out on the proxy, which called the refusing defineProperty trap, and the assignment failed with a TypeError even though the set trap itself allowed it. Without the receiver, the write went straight to the target and bypassed the defineProperty trap entirely, so y was stored. Which is correct depends on what you want, and the lesson is that the two traps are not independent. A rule enforced only in defineProperty is bypassed by a set trap that writes to the target, and a rule in both is checked twice.
What the engine enforces
The trap's answer must be consistent with the target's state.
const claims = new Proxy({}, {
defineProperty() {
return true;
},
});
try {
Object.defineProperty(claims, "fixed", { value: 1, configurable: false });
} catch (e) {
console.log("non-configurable:", e.name);
}
Object.defineProperty(claims, "soft", { value: 1, configurable: true });
console.log(Object.hasOwn(claims, "soft"));
const closed = Object.preventExtensions({});
const nope = new Proxy(closed, { defineProperty: () => true });
try {
Object.defineProperty(nope, "added", { value: 1 });
} catch (e) {
console.log("non-extensible:", e.name);
}non-configurable: TypeError
false
non-extensible: TypeErrorA trap may not claim to have created a non-configurable property that the target does not have, since the target could not enforce it. It may claim success for a configurable one, and the second case shows what that means: the trap said true and defined nothing, so Object.hasOwn() is false. And a non-extensible target cannot gain a property through a proxy, so claiming success for a new key is a TypeError.
A mistake we hit: a flag check that broke assignments
const strictFlags = new Proxy({}, {
defineProperty(target, property, descriptor) {
if (descriptor.writable !== true) {
throw new TypeError(`${String(property)} must be writable`);
}
return Reflect.defineProperty(target, property, descriptor);
},
});
strictFlags.count = 1;
try {
strictFlags.count = 2;
} catch (e) {
console.log(e.message);
}
console.log(strictFlags.count);count must be writable
1The rule was meant to forbid read-only properties. It rejected an ordinary update, because the second assignment reaches the trap as { value: 2 }, with writable missing. The fix is to test descriptor.writable === false, which is only true when the caller asked for it. We caught this in a test that incremented a counter twice. Treat absent fields as "not changed", and check only the fields that are present.
Choosing between the definition options
| You want | Use |
|---|---|
| To forbid accessors or hidden properties on an object | a defineProperty trap |
| To make an object immutable | Object.freeze() |
| To validate values on assignment | a [set trap](/library/javascript-proxy-set-trap) |
| To define one fixed property | Object.defineProperty() on a plain object |
| To watch every change including freezing | set, deleteProperty, defineProperty and preventExtensions traps together |
Practical rules we follow
Test only the descriptor fields that are present. Return Reflect.defineProperty(target, property, descriptor) for definitions you allow. Decide deliberately whether a set trap passes the receiver, and test an assignment through the finished proxy. Handle Object.freeze() by running it on a proxy in a test. Write one test per route to a property: defineProperty, assignment, Object.assign and freeze.
Where the defineProperty trap stops being the right tool
It is a low-level hook with many entry points, and it is rarely the simplest way to enforce a rule. For validation, set is clearer. For immutability, Object.freeze() is shorter. Its descriptors are partial, so every check needs to handle missing fields. And it is invisible to code that holds the raw target. The reference is in the MDN Proxy defineProperty handler page and the ECMAScript specification.


