In an attempt to make my libraries more portable, I read on AMD and CommonJS. The only thing I noticed above all is how they use the directory structure and strive to have one module for each file. From what I can say, their "namespaces" match the directory tree.
However, my own code uses the global object as a namespace, and then among my various files, regardless of directories, I add classes to this object.
(function (Twifty) {
return Twifty;
}(Twifty || {}));
During the upgrade process, I try to support AMD and CommonJs. There are many articles on how to do this, but I cannot wrap my head around the define function . This is how I convert the above code:
(function (root, factory) {
var Twifty = root.Twifty || {};
'use strict';
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof exports === 'object') {
factory(Twifty);
} else {
if (!document) {
throw 'twifty-map requires a DOM object';
}
root.Twifty = factory(Twifty);
}
}(this, function(Twifty) {
return Twifty;
}));
AMD define. factory Twifty, , . ?
, , .
, .