What is requireJS AMD in Magento2

Simply we can say, below format is the AMD module.

define(['jquery'] , function ($) {
    return function () {};
});

Module Vs AMD module

Javascript Module Pattern

(function () {
   this.myGlobal = function () {};
}());

In the javascript module pattern, the dependencies are assumed to be immediately available when this function executes. This limits the loading strategies for the dependencies. AMD module addresses these issues by the below way.

AMD Module

//Calling define with a dependency array and a factory function
define(['dep1', 'dep2'], function (dep1, dep2) {

    //Define the module value by returning a value.
    return function () {};
});
  • Register the factory function by calling define(), instead of immediately executing it.
  • Pass dependencies as an array of string values, do not grab globals.
  • Only execute the factory function once all the dependencies have been loaded and executed.
  • Pass the dependent modules as arguments to the factory function.

I hope it helps to grab the idea of what is AMD in Magento 2.

About Bilal

I'm a software programmer, I handle Magento2 modifications at any level.

View all posts by Bilal →

Leave a Reply

Your email address will not be published. Required fields are marked *