Constructors in JavaScript objects

后端 未结 19 1991
夕颜
夕颜 2020-11-22 10:21

Can JavaScript classes/objects have constructors? How are they created?

19条回答
  •  不要未来只要你来
    2020-11-22 10:32

    Yes, you can define a constructor inside a class declaration like this:

    class Rectangle {
      constructor(height, width) {
        this.height = height;
        this.width = width;
      }
    }
    

提交回复
热议问题