在 ES6 中通过代理捕获 class 定义

Trapping class definition via Proxy in ES6

是否可以设陷阱extends?或者在 class 中捕获定义?例如:

class B extends A {
    method1( ) { }
    static method2( ) { }
}


有什么方法可以捕获以下事件:


None 的现有机制似乎有效。尝试了 setPrototypeOfdefineProperty 个陷阱。

当 class B 扩展 class A 时,它得到它的 prototype 对象。因此,您可以在 get 上定义一个带有陷阱的代理,然后检查正在访问的 属性 是否为 "prototype"

class A {}

PA = new Proxy(A, {
  get(target, property, receiver) {
    console.log('get', property)
    if (property == 'prototype')
      console.info('extending %o, prototype=%s', target, target.prototype)
    return target[property]
  }
})

class B extends PA {}