In JavaScript, is constructor mandatory in a class?

后端 未结 2 1854
野性不改
野性不改 2021-02-07 02:16

I am reading about JavaScript class from the Mozilla documentation section of \'Class body and method definitions\'. Under the Constructor section, it states that

相关标签:
2条回答
  • 2021-02-07 02:44

    You should just write a class without a constructor and see if it works :)

    From the same docs

    As stated, if you do not specify a constructor method a default constructor is used. For base classes the default constructor is:

    constructor() {}
    

    For derived classes, the default constructor is:

    constructor(...args) {
      super(...args);
    }
    
    0 讨论(0)
  • 2021-02-07 02:58

    No, It is not necessary. By default constructor is defined as :

    constructor() {}
    

    For inheritance we use this constructor to call super class as :

    constructor() {
        super.call()
    }
    
    0 讨论(0)
提交回复
热议问题