In JavaScript, is constructor mandatory in a class?

后端 未结 2 1849
野性不改
野性不改 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);
    }
    

提交回复
热议问题