0

Is it possible to execute code upon inheriting a parent class? Removing the need to initialize the child class.

After fiddling around myself, and doing a week of searches, i came up empty.

    // This works, its will add the class in the registry array

    let registry = [];

    class Parent {
        constructor() { 
            registry.push(this) 
        }
    }

    class Child extends Parent {

    }

    let child = new Child();



    // What i wish i could do, note that the final line of code is removed. 
    // Removing the line stops the program from adding the class to the array,  
    // is it possible to achieve the same result as above without that line?

    let registry = [];

    class Parent {        
        constructor() { 
            registry.push(this) 
        }
    }

    class Child extends Parent {

    }
Dennis
  • 101
  • 2
  • 2
    If you don't instantiate the class, why would it be created (and hence adde to the array?) – Nick Martin Oct 17 '19 at 23:56
  • That i understand. So what you are saying is that a class can never initialize itself without a call from outside the scope of said class? – Dennis Oct 17 '19 at 23:59
  • 1
    Yes. It doesn't even make sense to do so. What [actual problem](https://meta.stackexchange.com/q/66377) are you trying to solve? Probably inheritance (or even classes as a whole) are the wrong tool for the job. – Bergi Oct 18 '19 at 00:06
  • 1
    @Dennis sure, you can instantiate the class from within the class, but you still need to instantiate the class for the internal class to be instantiated :S Check out this lengthy answer to a similar (I think) question: https://stackoverflow.com/questions/18360891/how-does-creating-a-instance-of-class-inside-of-the-class-itself-works – Nick Martin Oct 18 '19 at 00:07
  • @Bergi i remember using Unity3d, where upon inheriting from Monobehaviour the class and some functions (Start, Update etc...) would automatically be called. I am trying to mimic this behaviour. Never figured out how they did that, perhaps it was some compiler magic? Anyway, i want anything that inherits from that parent class to add itself to a registry simply because it inherits from that parent. – Dennis Oct 18 '19 at 00:13
  • @NickMartin Yup, just wondering if there is some fantastic workaround to achieve this behaviour anyway :P – Dennis Oct 18 '19 at 00:14
  • 1
    @Dennis "*i want anything that inherits from that parent class to add itself to a registry*" - no, don't do that, [your parent class should not have such a registry](https://softwareengineering.stackexchange.com/q/219543/66652). What did you plan to use that for? – Bergi Oct 18 '19 at 00:22
  • Thanks for the swift responses! I think i'm starting to wrap my head around why what i am trying to achieve shouldn't even be tried. Still not clear on why Unity3D does manage to get away with it, but the links you've provided already awnser more questions i have than i managed to find before, so i'm sure ill figure something out. Thanks again :D. – Dennis Oct 18 '19 at 00:59

0 Answers0