Skip to main content

Hi All,

I would like to understand the below statement from LWC documentation. 

The first statement must be super() with no parameters. This call establishes the correct prototype chain and value for this. Always call super() before touching this.

 

Please explain in as much detail as possible.

 

Thanks

4 个回答
  1. 2022年5月10日 04:25

    CHANDRA KANT BAGHEL if you are from OOPS background you must be knowing how inheritance works. when we write a constructer the first statement should be Super(). here super means that The Parent constructor of current comp will run this is needed because in inheritance we inherit the attributes etc from parent and now those values are initialized in the constructor so if you don't invoke the parent constructor those values might now initialize which the child comp is inheriting hence Child comp will not have correct values. this is why you need to Call the Parent constructor before you execute the child constructor.

     

    Every LWC Extends a LightningElement so we must call the Parent constructor

     

    export default class MetadataUtlity extends LightningElement {

    }