JavaScript pattern for multiple constructors

前端 未结 9 988
不知归路
不知归路 2020-12-07 13:12

I need different constructors for my instances. What is a common pattern for that?

9条回答
  •  难免孤独
    2020-12-07 13:39

    Sometimes, default values for parameters is enough for multiple constructors. And when that doesn't suffice, I try to wrap most of the constructor functionality into an init(other-params) function that is called afterwards. Also consider using the factory concept to make an object that can effectively create the other objects you want.

    http://en.wikipedia.org/w/index.php?title=Factory_method_pattern&oldid=363482142#Javascript

提交回复
热议问题