Why do I need to access the base class constructor with super()?

Svetan Dimoff

Why do I need to reference the base class' constructor in the subclass? I seem to get the same result either way.

class Animal {
    constructor(age) {
        this.age = age;
    }
}

class Mammal extends Animal {}

let lion = new Mammal(3);
lion.age //=> 3

as opposed to

class Mammal extends Animal {
    constructor(){
        super(3)
    }
}

let lion = new Mammal();
lion.age //=> 3
T.J. Crowder

If you don't provide an explicit constructor, in a subclass1 the JavaScript engine will generate one for you that looks like this:

constructor(...args) {
    super(...args);
}

That's what's going on in your first example. So this:

class Mammal extends Animal {
}

is the same as this:

class Mammal extends Animal {
    constructor(...args) {
        super(...args);
    }
}

...it's just that the second is explicit, whereas the first is using the constructor inserted into the class for you by the JavaScript engine.

This is defined in Step 10 of §14.5.14: Runtime Semantics: ClassDefinitionEvaluation:

  1. If constructor is empty, then

    • If ClassHeritageopt is present, then

      • Let constructor be the result of parsing the source text

        constructor(... args){ super (...args);}
        

        using the syntactic grammar with the goal symbol MethodDefinition.

    • Else,

      • Let constructor be the result of parsing the source text

        constructor( ){ }
        

        using the syntactic grammar with the goal symbol MethodDefinition.


1 If it's not a subclass, as you can see above, it's just constructor() { }.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why do I need a cast to access the actual value from base class?

From Dev

Why do I need another constructor in an extended abstract class?

From Dev

why do I need super() in Android?

From Dev

Why I cannot call the base class Constructor?

From Dev

Why set up a constructor in a class to set a base parameter when I could do the same in a class it inherits from?

From Dev

Why set up a constructor in a class to set a base parameter when I could do the same in a class it inherits from?

From Dev

Do I need virtuals in base class only?

From Dev

How do I call on the super class' constructor and other statements in Dart?

From Dev

Why does a private base class constructor result in "Implicit super constructor is not visible"

From Dev

Do I need to declare a constructor and destructor in a new class?

From Dev

do I need every method in a class to be in the constructor for subclasses to inherit?

From Dev

How do I access an inner class constructor from a derived class?

From Dev

Why do I need to have a constructor in this Java generic subclass?

From Dev

Why do I need to write a constructor here explicitly?

From Dev

Why do I need to write a constructor here explicitly?

From Dev

What is a base class constructor and why would I want to use it? (with Example)

From Dev

Why do we need a constructor?

From Dev

Why do abstract derived classes need to initialize a virtual base class?

From Dev

Why do abstract derived classes need to initialize a virtual base class?

From Dev

Why can I not access an instance of class that is created in a constructor ( Java )

From Dev

Kotlin Constructor from Java Super Class Constructor Access

From Dev

How do I access an array list inside of a class constructor in java?

From Dev

How do I access a private constructor in a separate class?

From Dev

Access method of second base class with super()

From Dev

How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

From Dev

How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

From Dev

why does the default constructor in MyClass calls super i.e of Object class

From Dev

Why do I need a no-args constructor to use ApplicationScoped beans with Constructor injection within CDI?

From Dev

PyQt4: why do we need to pass class name in call to super()

Related Related

  1. 1

    Why do I need a cast to access the actual value from base class?

  2. 2

    Why do I need another constructor in an extended abstract class?

  3. 3

    why do I need super() in Android?

  4. 4

    Why I cannot call the base class Constructor?

  5. 5

    Why set up a constructor in a class to set a base parameter when I could do the same in a class it inherits from?

  6. 6

    Why set up a constructor in a class to set a base parameter when I could do the same in a class it inherits from?

  7. 7

    Do I need virtuals in base class only?

  8. 8

    How do I call on the super class' constructor and other statements in Dart?

  9. 9

    Why does a private base class constructor result in "Implicit super constructor is not visible"

  10. 10

    Do I need to declare a constructor and destructor in a new class?

  11. 11

    do I need every method in a class to be in the constructor for subclasses to inherit?

  12. 12

    How do I access an inner class constructor from a derived class?

  13. 13

    Why do I need to have a constructor in this Java generic subclass?

  14. 14

    Why do I need to write a constructor here explicitly?

  15. 15

    Why do I need to write a constructor here explicitly?

  16. 16

    What is a base class constructor and why would I want to use it? (with Example)

  17. 17

    Why do we need a constructor?

  18. 18

    Why do abstract derived classes need to initialize a virtual base class?

  19. 19

    Why do abstract derived classes need to initialize a virtual base class?

  20. 20

    Why can I not access an instance of class that is created in a constructor ( Java )

  21. 21

    Kotlin Constructor from Java Super Class Constructor Access

  22. 22

    How do I access an array list inside of a class constructor in java?

  23. 23

    How do I access a private constructor in a separate class?

  24. 24

    Access method of second base class with super()

  25. 25

    How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

  26. 26

    How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

  27. 27

    why does the default constructor in MyClass calls super i.e of Object class

  28. 28

    Why do I need a no-args constructor to use ApplicationScoped beans with Constructor injection within CDI?

  29. 29

    PyQt4: why do we need to pass class name in call to super()

HotTag

Archive