When should I automatically create an object even if `new` is forgotten?

前端 未结 4 643
無奈伤痛
無奈伤痛 2020-11-30 11:05

Let us say I have the following object constructor:

function Foo(bar) {
    this.bar = bar;
}

If I run the function in the global scope wit

4条回答
  •  北荒
    北荒 (楼主)
    2020-11-30 11:51

    Advocates of "readability" might describe it as an anti-pattern but you know your own code better than anyone else - if it works for you then go for it. Personally I'd rather have one way of doing one thing - it makes it clear and concise. If another developer enters the fray and tries to use my object with out instantiating it I'd rather it throw an error to tell him/her they are being daft.

提交回复
热议问题