Constructors in JavaScript objects

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

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

19条回答
  •  日久生厌
    2020-11-22 10:46

    So what is the point of "constructor" property? Cannot figure out where it could be useful, any ideas?

    The point of the constructor property is to provide some way of pretending JavaScript has classes. One of the things you cannot usefully do is change an object's constructor after it's been created. It's complicated.

    I wrote a fairly comprehensive piece on it a few years ago: http://joost.zeekat.nl/constructors-considered-mildly-confusing.html

提交回复
热议问题