Effective [removed] Making your Constructor Function new-agnostic

前端 未结 2 933
青春惊慌失措
青春惊慌失措 2020-12-11 21:33

I\'ve been reading \'Effective JavaScript\' lately and I came across this question.

The author explains how it\'s important to make your Constructor Function new-ag

2条回答
  •  北海茫月
    2020-12-11 22:08

    Yes, the effect is the same, however, it allocates one more object. The constructor is meant to be used with new, and this technique takes car of the cases where the programmer has forgotten the new.

    It would be better to throw an exception in the latter case if you ask me.

提交回复
热议问题