JavaScript Learning Notes (scatter)--inheritance, constructor super

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.