2

I write different files under JavaScript Module Patern like this:

file 1.js

var Module = (function (module) {

  module.function = function () {

    console.log(this.thirdFunction) // This is my issue. I cannot access the function from the third file because it is concatenated in a way that the function still not exist.  

  };

  return module;

}(Module || {}));

some-folder/file 2.js

var Module = (function (module) {

  module.somethingElse = function () {


  };

  return module;

}(Module || {}));

whatever/file 3.js

var Module = (function (module) {

  module.thirdFunction = function () {


  };

}(Module || {}));

I put all these files in different directories, names in a different time. Then I am using concatenating tool to have one file and then I use it in my html file. But I am facing trouble that I cannot resolve.

To have all these working, I have to include them in a specific way and order to call functions from whatever I need and to re-order files when something is not yet defined/created in the returned final object. If I have 100 files and folders it will be a trouble for me again.

Do I understand this right: http://gruntjs.com/configuring-tasks#globbing-patterns that I have manually to order files, folders and everything in my grunt tasks?

I do not want to use AMD tools, just plain JavaScript files and some approach to hack the order requirements. If there is no any easy idea for me to you, I would try the AMD tools like require.js but I am not sure if these kind of tools can help with this lame problem. I would appreciate some grunt tool, some files/folders names conventions, anything that would not force me to install more and more tools. Thank you in advance!

Another thing that bothers me is the following:

If I want to isolate code but I do not have to return object property in the final object, is it alright to do something like this:

file 4.js

var Module = (function (module) {

 var someThing = Module.somethingElse() // from file 2.js
 and then using someThing here for clicking, DOM rendering, etc, etc 

}(Module || {}));

Is it stupid to stick to the same var Module conventions for files where I actually do not return anything? I just think of way how to avoid the object name and using this again

Alister Greenpan
  • 133
  • 2
  • 11

1 Answers1

0

Indeed, AMD tools were created just for this kind of problem. However you can work around this to some extent with grunt. You could simply organize the files that need to go first into a folder and list them out in the order you want, and then have another folder containing all files who's order doesn't matter, which will include everything.

'js/main/First.js',
'js/main/Second.js',
'js/rest/*.js'

No matter what you choose for this project, you might want to look into Require.js or Browserify for future work.

GMchris
  • 5,439
  • 4
  • 22
  • 40
  • Thank you, GMchris! Can you give me some note about my dilemma from the last paragraph? Btw, if you have used IE6 with modular importing, can this somehow fix this issue or it would be the same there as well? – Alister Greenpan Feb 03 '16 at 10:03
  • You mean using var Module if you're not returning anything? The main point of modules is to keep the global namespace clean and that you can decide what is public and what is private, which you can still do by simply attaching whatever needs to be public to the Module object, although it is strongly recommended that you use a return {//All your public members}; instead. A module without a return is just a closure. And it's really handy for initialization and setting up events and their handlers. As for the point about IE6, I'm afraid I don't know enough on the subject to really be of any help. – GMchris Feb 03 '16 at 10:30
  • So you would advice someone using this "A module without a return is just a closure. And it's really handy for initialization and setting up events and their handlers." or not? I didn't get it. `var MyDOs = (function (module) {...` with returning nothing or: `(function () {...` – Alister Greenpan Feb 04 '16 at 14:49
  • If you're not returning anything nor are you setting a value to the MyDOs variable object inside the closure, just go with the `(function() {...` variant. – GMchris Feb 04 '16 at 14:53