What do you think of commonJS vs Asynchronous Module AMD
Posted By : Milind Ahuja | 22-Apr-2018
JavaScript Modules are small units of code, which is independent and reusable. Their functionality is distinct, which allows them to be added and removed without the system
Modules are self-contained. If a module is decoupled from other pieces of code, it is much easier to update it. This makes the programmer a lot less intimidating to go through the program. It solves the namespace ambiguity allowing the objects to be created in publicly accessible namespaces while the functions in it are private. Modules are reusable which eliminates the duplicacy in code and saves
The Reavling Module Pattern was getting used before the modules arrived.
var revealingModule = (function () {
var privateVar = "Braun Strohman";
function setNameFn( strName ) {
privateVar = strName;
}
return {
setName: setNameFn,
};
})();
revealingModule.setName( "Paul Adams" );
In the above program, the public functions are exposed whereas the private properties and methods are encapsulated.
A single file can contain multiple modules but the drawbacks are that the modules do not load asynchronously and it is not possible to import modules programmatically.
CommonJS
Then came a separate approach to interact with the module system which uses the keyword require and exports. require is a function which is used to import functions from another module, on the other side, exports is an object where you can export any function which is put into it.
//------ payments.js ------
var customerStore = require('store/customer'); // import module
//------ store/customer.js ------
exports = function(){
return customers.get('store);
}
In the example above, the customerStore is imported to payment.js. The function which is set to the exports object in customer module is loaded in payment file.
These modules are
CommonJS basically specifies that you need to have a require() function in order to fetch dependencies, an export method to export the contents from another module and a module identifier (describes the module location) that is used to require the dependencies.
Asynchronous Module Definition (AMD)
AMD was born to replce commonJS as it was not browser compatible. As the name indicates, it supports asynchronous loading of the module.
define(['module1', ',module2'], function(module1, module2) {
console.log(module1.setName());
});
The requested modules are loaded in a non-blocking manner in the background and once the loading is completed, the callback function is executed.The define function takes the first argument as an array of dependency modules. These modules can be functions, objects,
RequireJS implements AMD API. The plain javascript files and modules are loaded by using script tags. An optimizing tool is included in it which can be run while deploying our code for better performance.
Now we know what commonJS and AMD are. Now let's talk about the difference between them.
AMD and commonJS, both are Javascript module loader.
AMD is browser
While
I hope this
THANKS
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Milind Ahuja
Milind is a bright Lead Frontend Developer and have knowledge of HTML, CSS, JavaScript, AngularJS, Angular 2+ Versions and photoshop. His hobbies are learning new computer techniques, fitness and interest in different languages.