0

Code:

(function () {
    "use strict";

    var proxy;
    var realTarget;
    var target;

    realTarget = {};
    target = {};

    proxy = new Proxy(target, {
        isExtensible: function (target) {
            return Reflect.isExtensible(realTarget);
        },
        preventExtensions: function (target) {
            return Reflect.preventExtensions(realTarget);
        }
    });

    Object.freeze(proxy);
})();

Resulting error message:

[error] Line 20: Uncaught TypeError: 'preventExtensions' on proxy: trap returned truish but the proxy target is extensible

Why is this an error? What's the point of a proxy if I can't redirect this operation?

Melab
  • 2,594
  • 7
  • 30
  • 51

1 Answers1

1

Proxy traps have a set of so-called invariants they have to conform to.

One of these is that the preventExtensions trap may only return true if [[IsExtensible]] of the proxy's target returns false (i.e. if the target is really made inextensible).

You can work around this by returning false (signaling that making the object inextensible has failed):

preventExtensions: function (target) {
    Reflect.preventExtensions(realTarget);
    return false
}
FZs
  • 16,581
  • 13
  • 41
  • 50