It should be something like:
class C { static m() { console.log('hi') } } // automatically run C.m class D extends C { } A python case is found. Is this possible in JavaScript?
It should be something like:
class C { static m() { console.log('hi') } } // automatically run C.m class D extends C { } A python case is found. Is this possible in JavaScript?
Javascript doesn't have a metaclass per se, but you can create a pseudo metaclass by defining a class within a function:
function C() { class Cls { static m() { console.log('hi') } } Cls.m() return Cls } class D extends C() {} // prints 'hi' extendedClass.onExtend() after the class definition.This can be solved using custom transpiler.
I have made a babel plugin library to solve this problem.
class Y extends X {} // This package will always add this after every class that extends other class X?.onExtend?.(Y) You can see the babel plugin implementation here
prototypeproperty a getter or use a proxy that listens on get operations of theprototypeproperty. The lineclass D extends C {}does invoke a read operation on the propertyC.prototype. But the next challenge would be to differentiate this situation from other accesses of that property (e.g. duringnew C()or manualy access).