[removed] The Good Parts - How to not use `new` at all

前端 未结 9 1929
迷失自我
迷失自我 2020-12-07 09:15

Crockford\'s book, JavaScript: The Good Parts, says (on page 114) that constructor functions should always be given names with an initial capital letter (i

9条回答
  •  情话喂你
    2020-12-07 09:52

    You can avoid new by creating factory functions:

    var today = Date.getToday();
    

    (In case you were wondering, you can't avoid it on the factory function itself:)

    Date.getToday = function() { return new Date(); };
    

    Although I only think you should create such functions if it adds semantic value (as in the case above) or if you can default some of the constructor parameters. In other words, don't do it just to avoid using new.

提交回复
热议问题