Module pattern vs. instance of an anonymous constructor

前端 未结 4 2129
醉话见心
醉话见心 2021-02-01 17:09

So there\'s this so-called module pattern for creating singletons with private members:

var foo = (function () {
    var _foo = \'private!\';
    return         


        
4条回答
  •  时光说笑
    2021-02-01 17:52

    I don't see any substantial difference between the two. I would prefer the latter simply since it has much less punctuation clutter.

    On the other hand the "module pattern" approach seems to be the most common and is becoming a well known pattern (due to the rise in JQuery). It might be better to stick with this recognised pattern not because it has any more technical merit than the other approach but just because it will likely be more recognizable (despite the heavy punctuation).

提交回复
热议问题