Super in constructors
Today read "JavaScript Design patterns and development practices," in the book to see a piece of code appeared super statement, the first time to see the keyword, so the internet to check its role, found that the key is from Java, the source does not delve into, say its purpose. Super's main function is to call the parent's constructor, but the confusion is that since it's a constructor, it's already been called at the time of declaration, why is super called? Self-tapping a piece of code deepened the understanding that:
This creates a animal class that contains a constructor
Next create the subclass of Animal Dog, which contains a speak function (for understanding inheritance ),
The DDD class is then created to inherit from the dog class, and the constructor in DDD uses the Super parameter of 123.
And then an instance of a dog called DD
Now look at what happens when the instance is DD:
At this time the console in animal appeared, and the Super parameter 123 was printed, and DDD was called to the animal constructor. Now comment out the DDD constructor to see the effect.
Now it is not printing anything, not because animal's constructor is not called by super, but if the animal in the If judgment to QQQ and there is no actual value, so no printing, in order to confirm this, now the animal in the if judgment removed.
The undefined is now printed because the constructor parameter in animal is QQQ and there is no actual value passed in, so there is no definition.
Right now
The first thing to note is that super can only be used in constructors.
JavaScript Learning Notes (scatter)--inheritance, constructor super