Asynchronous Module Definition (AMD)

AMD is a specification for implementing modules in the browser. AMD is designed by keeping browser limitations in mind, that is, it imports modules asynchronously to prevent blocking the loading of a webpage. As AMD is not a native browser specification, we need to use an AMD library.

RequireJS is the most popular AMD library. Let's see an example of how to create and import modules using RequireJS. According to the AMD specification, every module needs to be represented by a separate file. So first, create a file named math.js that represents a module. Here is the sample code that will be inside the module:

define(function(){   const sum = (x, y) => x + y  const sub = (x, y) => x - y  const math = {  findSum(a, ...

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.