Not own property getters

T.J. Crowder tj.crowder at
Fri Sep 8 07:50:31 UTC 2017

On Fri, Sep 8, 2017 at 2:28 AM, Alex Kodat
<alexkodat at> wrote:
> Head slap. The following does what I want with essentially zero
> overhead (at least with V8)...

:-) Clever solution for situations where you can't detect these statically
in advance, nice one.

You can turn it up to 11 by avoiding hitting the proxy for the
`` properties, by making the base guard prototype derive
from your proxy and put the `` features on it. That way,
`hasOwnProperty`, `valueOf`, etc. aren't impacted by passing through the
proxy. (See my `Guarded` below.)

You'll also want to handle `toJSON` and other spec-defined optional
properties or properties implementations commonly look for outside spec (is
that what your `inspect` property is?). And set `constructor`.

function Guarded() {
Guarded.prototype = Object.create(new Proxy(Object.freeze(Object.create(null)),
  get: (target, prop) => {
    if (prop === "toJSON" || prop === "inspect") {
        return undefined;
    throw Error("Bad property: " +  prop);
Object.defineProperties(Guarded.prototype, Object.getOwnPropertyDescriptors(
Object.defineProperty(Guarded.prototype, "constructor", {
    value: Guarded,
    configurable: true

class Foo extends Guarded {}

-- T.J. Crowder
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list