handler.isExtensible(target) is a trap that runs when JavaScript asks a Proxy whether new properties can be added to it. This happens with Object.isExtensible() and Reflect.isExtensible(), and also behind the scenes in Object.isFrozen() and Object.isSealed().
This trap is the most limited one. Its answer must match the target, so you cannot change the result. What the trap is good for is noticing that the question was asked, for example to log it. To answer, use Reflect.isExtensible(target).
Remember
const handler = {
isExtensible(target) { ... }
};
target → the original object
return true or false
The answer MUST equal Object.isExtensible(target)
or JavaScript throws a TypeError
Syntax
Add an isExtensible trap to the handler of a Proxy.
JavaScript
const proxy = new Proxy({}, {
isExtensible(target) {
console.log("Someone asked");
return Reflect.isExtensible(target);
}
});
console.log(Object.isExtensible(proxy)); // Someone asked, then true
Call
What it does
Object.isExtensible(proxy)
Asks directly. Runs the trap.
Reflect.isExtensible(proxy)
The function-style version.
Object.isFrozen(proxy), Object.isSealed(proxy)
Both check extensibility first, so they run the trap.
Object.preventExtensions(proxy)
Does not run this trap. It uses preventExtensions.
Open objects are not frozen. The first two tests ask the trap, get true (extensible), and can answer false right away.
Freeze the Proxy.Object.freeze(proxy) locks the target and makes its property read-only. The question counter stays at 2, because freezing does not ask whether the object is extensible.
Now it is frozen. The test asks the trap again (now 3 questions), gets false and then confirms that the property cannot change.
Example 3 — The Answer Must Match the Target
See what happens when the trap gives an answer that is different from the target. The catch blocks print the error type only.
JavaScript
const liar = new Proxy({}, {
isExtensible() {
return false;
}
});
try {
Object.isExtensible(liar);
} catch (err) {
console.log("False for an open target: " + err.name);
}
const locked = Object.preventExtensions({});
const liar2 = new Proxy(locked, {
isExtensible() {
return true;
}
});
try {
Object.isExtensible(liar2);
} catch (err) {
console.log("True for a locked target: " + err.name);
}
const truthy = new Proxy({}, {
isExtensible() {
return 1;
}
});
console.log("Number 1 counts as true: " + Object.isExtensible(truthy));
False for an open target: TypeError
True for a locked target: TypeError
Number 1 counts as true: true
How It Works
False for an open target. The target can still grow, so the real answer is true. A trap that says false is caught with a TypeError.
True for a locked target. The opposite mistake is caught too. A locked target must answer false.
Truthy is allowed. The number 1 is turned into true, which matches the open target. It works, but always return a real boolean.
Compatibility
Browser & Runtime Support
handler.isExtensible() 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.isExtensible()
Supported in Chrome 49+, Edge 12+, Firefox 49+, Safari 10+, Opera 36+ and Node.js 6+. Internet Explorer never got it.
ES2015Standard since
Google Chrome49+ · Desktop & Mobile
Full support
Microsoft Edge12+ · Chromium and legacy
Full support
Mozilla Firefox49+ · Desktop & Mobile
Full support
Apple Safari10+ · macOS & iOS
Full support
Opera36+ · Chromium based
Full support
Samsung InternetRecent versions · Android
Supported
Android WebView49+ · Chromium based
Supported
Node.js6+ · Server runtime
Supported
DenoSupported · JavaScript runtime
Supported
BunSupported · JavaScript runtime
Supported
Internet ExplorerNo native support
Not supported
handler.isExtensible()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 JavaScript asks a Proxy whether new properties can be added, for example with Object.isExtensible() or Reflect.isExtensible().
No. The answer must be the same as Object.isExtensible(target), or JavaScript throws a TypeError. The trap is useful for logging and side effects, not for changing the result.
Object.isExtensible(), Reflect.isExtensible(), and also Object.isFrozen() and Object.isSealed(), because they check extensibility first.
No. It runs the preventExtensions trap. The isExtensible trap is only for questions about the state.
Because extensibility is a promise that cannot be broken. Once an object is locked, code must be able to trust that no property will be added. The check stops a Proxy from lying about it.