skip to Main Content

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?

2

Answers


  1. In JS, you can’t automatically run code when a subclass is declared.

    Login or Signup to reply.
  2. 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'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search