官术网_书友最值得收藏!

Using ES2015 modules

So far, we have mentioned that models are just plain classes. An ES2015 module is just one file. Within that file lives both public and private constructs. Things that are private are only visible within that file. Things that are public can be used outside said file. In Angular, Es2015 modules aren't used only for models but for all imaginable constructs such as components, Directives, Pipes, Services, and so on. This is because ES2015 modules are an answer to how we split our project into smaller parts, which provides us with the following benefits:

  • Many small files makes it easier to parallelize the work you do and have many developers work on it at the same time
  • The ability to hide data by, making some parts of your application public and some other private
  • Code reuse
  • Better maintainability

We have to remember what web development used to look like to understand these statements. When the web was young our JavaScript code more often than not consisted of one file. That quickly became a huge mess. There have been different techniques over the years to find a way to split up our app into many small files. Many small files have made it easier to maintain and also to get a good overview of what is going on, among many other benefits. There have been other issues though. As all these small files had to be stitched back together before being shipped with the app, a process called bundling, we suddenly had one giant file where functions and variables could by mistake affect each other due to naming collisions. A way to attack that problem is to deal with something called information hiding. This to ensure the variables and functions we created are only visible to certain other constructs. There are multiple ways, of course, to address this issue. ES2015 has a private by default way about them. Everything declared in an ES2015 is private by default unless you explicitly export it, thereby making it publicly accessible to other modules that import the aforementioned module. 

So how does this connect to the previous statements? Any module system really allows us to maintain visibility in our project as it grows with us. The alternative is one file which is complete chaos. As for several developers working at the same time, any way of logically dividing up the app makes it easier to divide up the workstreams between developers. 

主站蜘蛛池模板: 阳西县| 饶平县| 兴化市| 凌源市| 安仁县| 禹州市| 北安市| 乌兰察布市| 抚松县| 东光县| 沙河市| 右玉县| 蒲江县| 淳安县| 中超| 济宁市| 怀仁县| 泽库县| 丹凤县| 招远市| 玉溪市| 汪清县| 崇阳县| 财经| 杨浦区| 犍为县| 行唐县| 广德县| 刚察县| 高淳县| 白银市| 高淳县| 溧阳市| 正蓝旗| 东源县| 卫辉市| 石阡县| 乌苏市| 肃宁县| 余庆县| 广州市|