0

I'm a newbie to JavaScript and recently pick up a project with many JavaScript files. Previous guy on this job seemed get all the way down using a lot of global variables rather than any design pattern.

It is hard for maintaining as referring global variables everywhere. and can see private info in console by directly typing global variable names.

So my question is how should I do in further development? I would much like my future code more organized and modularized.

I read about there is no module definition in JavaScript and I can

  1. Using module pattern or Revealing module pattern

  2. Using Requirejs / commonjs/ webpack maybe?

I don't know, After reading about docs, I'm still confused that does it make much difference if I use module pattern by myself or introducting another requirejs? They all seem to be able to get my code modularized.

Qiang
  • 1,468
  • 15
  • 18

1 Answers1

1

I have another option for you to try - es6 modules which is not fully supported yet, but you can accomplish this by including Babel or Traceur in you project.

Requrejs is a good option either. We do use it in our large project with tons of js files and it works pretty good. Though there are some specific bottlenecks, it's pretty clear explained, how you can avoid those. As an example let's take Backbonejs which is not AMDish library. AMDish I call some module or library which is defined correctly for requiring them into other modules. You can read about making your AMD modules correctly here: http://requirejs.org/docs/api.html (actually you can see everything you need here. I suggest you reading API before you do anything with your code. While some tutorials are pretty good point to start, but often they do not provide something specific and it can bring you a lot of pain until you read the link above. I was in this boat couple of weeks ago..)

Another pitfall is that some libraries, like underscore.js will still appear in global scope after you include it as AMD module. Still, you can look for workarounds to avoid it, if you really need.

Also requirejs does it's job pretty good in adding library dependencies using shims.

So, yes, you definitely can use requrejs, however there are some opinions why you shouldn't - http://codeofrob.com/entries/why-i-stopped-using-amd.html

So as with all frameworks and libraries it's a matter of personal preference which one to use. From my point of you if you want to use requirejs - go ahead and don't forget to read the API!

aprok
  • 1,147
  • 11
  • 25
  • Hi, thanks for you quick reply. And also, I found [this module pattern answer](http://stackoverflow.com/a/1841971/3462558). Is it much different between using `modular pattern` and `AMD module`. I can't see the reason why I need to add one more dependency. – Qiang Sep 03 '15 at 21:53
  • Actually you don't have to. It's just a convenient tool for managing you dependencies. Requirejs is written in javascript, it means you can write the same thing in javascript. The only one note here is this: everything you write yourself you debug yourself. Everything already baked for you was used thousands times and it's easier to find answers "why this doesn't work". So it's up to you! – aprok Sep 03 '15 at 22:04