是否可以诱捕extends?或者在一个类中捕获定义?例如:
class B extends A {
method1( ) { }
static method2( ) { }
}
有什么方法可以捕获以下事件:
B扩展A。method1( )被定义在B.prototypemethod2( )定义于B。
现有的机制似乎都不起作用。尝试setPrototypeOf和defineProperty陷阱。
是否可以诱捕extends?或者在一个类中捕获定义?例如:
class B extends A {
method1( ) { }
static method2( ) { }
}
有什么方法可以捕获以下事件:
B扩展A。method1( )被定义在B.prototypemethod2( )定义于B。
现有的机制似乎都不起作用。尝试setPrototypeOf和defineProperty陷阱。
当类B扩展类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 {}