Constructor function vs Factory functions

后端 未结 7 955
清酒与你
清酒与你 2020-11-22 06:52

Can someone clarify the difference between a constructor function and a factory function in Javascript.

When to use one instead of the other?

7条回答
  •  轮回少年
    2020-11-22 07:19

    A constructor returns an instance of the class you call it on. A factory function can return anything. You would use a factory function when you need to return arbitrary values or when a class has a large setup process.

提交回复
热议问题