Closures vs. classes for encapsulation?

前端 未结 3 990
我在风中等你
我在风中等你 2021-02-02 10:10

I\'m new to JS (from C++/etc), and it\'s just occurred to me that closures seem to be a simpler and more convenient way to handle encapsulation than classes. This code seems to

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-02 10:40

    The benefit of a function for the purposes of encapsulating functionality is that you can use the module pattern:

    http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth

    The module pattern provides the capability of creating private members and methods, without something with a lot of overhead like ease.js:

    http://easejs.org/

提交回复
热议问题