0

I want to pass child class instance to super class using super's constructor but I'm get this error

super(this);  

this is not allowed before superclass constructor invocation

Why i'm getting this error , also how could I resolve this issue


class Parent
{
    constructor(child)
    {
        this.child = child;
    }

    //...somewhere in code
    //child.doSomething();
}


class Child extends Parent
{
    constructor()
    {
        super(this);   // <==== the error here
    }

    doSomething = () =>
    {
        //...
    }
}
Ali Faris
  • 17,754
  • 10
  • 45
  • 70
  • 3
    That's not how you implement inheritance in JavaScript (or in general); that's more like *composition*. In inheritance terms, if `doSomething` only exists on `Child`, it shouldn't be getting called from `Parent` (as not all subclasses are required to have it). In composition terms, `Child` doesn't need to `extend Parent`, and shouldn't be responsible for passing itself to the constructor. – jonrsharpe Aug 17 '18 at 13:30
  • 1
    Setting `this.child = this` makes no sense. What is your goal? You can just call `this.doSomething()` – Bergi Aug 17 '18 at 13:31
  • 1
    Or maybe you wanted `class Child { constructor() { this.parent = new Parent(this); } … }`, instead of `extends Parent`? – Bergi Aug 17 '18 at 13:34
  • @Bergi , I'm developing react app and I come to situation where I need this kind of solution. `this.child` is not `super.child` as far as I know.(by the way I'm kind of new to javascript) – Ali Faris Aug 17 '18 at 13:37
  • 1
    @Ali You might want to post a new question where you post your actual code with your actual problem. Yes, `this.child` is the same in both parent and child methods/constructors when you call them on the *same child instance*. Are you coming from a different language where you need to distinguish attributes by the class that you "inherited" them from? – Bergi Aug 17 '18 at 13:41
  • @Bergi yes composition seems better solution than inheritance. I'm coming form java and I would use abstract class to solve my problem. – Ali Faris Aug 17 '18 at 13:51
  • 2
    @Ali You can have [something like abstract `class`es in JS](https://stackoverflow.com/q/29480569/1048572), but you [really don't need them](https://stackoverflow.com/a/4082496/1048572) – Bergi Aug 17 '18 at 14:17

2 Answers2

5

There's no need to pass this to super() because this inside the superclass constructor will be a reference to the same object. Recall that your class hierarchy will cooperate to perform initialization on a single new object.

Calls to super() must come before any reference to this, including in the super() argument list. Why? Because in order to mimic behavior of other OO languages, it must be the case that the top-most initializer in the class hierarchy gets its hands on the new object first. The parent (or "senior") initializer should be able to assume that prototype methods at that level have the semantics the base class expects, since it doesn't "know" what subclasses might have done with their prototypes etc. If a subclass initializer could modify the new object and override a base class prototype method (or something else of that flavor), it'd be chaos.

Pointy
  • 405,095
  • 59
  • 585
  • 614
2

In the child's constructor method, the parent's constructor must be called before accessing this. The superclass' constructor will have access to this anyway, since you are constructing an instance of the superclass - though it will not have access to any other initialization that your child constructor may yet do.

Alexander van Oostenrijk
  • 4,644
  • 3
  • 23
  • 37