Asynchronous module definition

from Wikipedia, the free encyclopedia

Asynchronous module definition ( AMD ) is a JavaScript - programming interface that enables modules can be loaded asynchronously and their dependencies. It thus maps two fundamental concepts of software development, modularization and reuse, in the otherwise functional and monolithic JavaScript environment. The modularization allows the division of a Javascript application into individual subcomponents, which can be developed and tested separately (divide and conquer ). Due to clear interfaces, AMD modules can be reused in other software projects.

The procedure is largely based on the inversion of control - design pattern (IoC pattern). The module corresponds to the term bean in IoC. This enables JavaScript to be built up modularly. The result are modules that are comparable to Java - classes are, and can be just as extended by inheritance. Each module must be saved in its own file - analogous to the Java class .

The advantages of modularization are particularly evident in the browser environment, where JavaScript is used particularly frequently. Only the required modules are loaded, not everything, as would be the case with synchronous loading. On the one hand, this increases the performance of the code and, on the other hand, makes debugging easier , especially in the case of cross-domain access problems. Furthermore, it enables a better reusability of individual code fragments without having to transport them by copying and pasting or server-side concatenation . This results in a reduction in the use of global variables to a minimum and thus significantly reduces the problems caused by namespace pollution.

However, the specification of the distribution of the code over many individual files also has disadvantages. Each file must by the browser in a separate HTTP load invocation, which in many small files much protocol - overhead can produce. This can lead to a noticeable delay , especially for connections with high latency . However, this overhead when calling up various Javascript files can be compensated for by using a server-side bundling.

Various AMD frameworks exist to implement the AMD format, such as B. RequireJS , The Dojo Loader or curl.js .

commitment

The AMD format provides a special module definition or component definition which enables the declaration and correct loading of dependencies. The module definition is therefore based on programming conventions. This convention has changed in many freely available Javascript libraries, such as B. jQuery or Socket.IO enforced.

define("Name des Moduls", ["Abhängigkeit1", "Abhängigkeit2"], factory);

The program code above illustrates the module definition in AMD format. In addition to the name of the module and the dependencies, a factory method is defined via which the module is generated. This factory method is used to instantiate the component and to export possible interface objects. As can be seen, a Javascript library is required for the use of the AMD format, which offers the define method and instantiates the various components as well as injecting the dependencies. In the AMD environment, this is referred to as an AMD loader. The procedure largely corresponds to the pattern of the abstract factory and the dependency injection with an IoC container. In this way, any components can be instantiated and the required dependencies can be provided in the appropriate order. The individual components of a JS application can be decoupled from one another.

define('PieChartModule', ['area', 'graph'], 
      function ( area, graph ) { 
          // Beschreibung des Plot-Modules
          var plotModuleExport = {
             plot: function(width, height, data){
	       // Einfache Nutzung der Module “graph” und “area”
               return graph.drawPie(area.randomGrid(width, height), data);
             }        
          };
          return plotModuleExport;
      };
);

Web links

Individual evidence

  1. https://docs.jboss.org/author/display/GTNPORTAL34/GDG-Asynchronous+Module+Definition
  2. http://docs.weejot.com/developer/reference/amd.html
  3. https://github.com/amdjs/amdjs-api/wiki/AMD
  4. http://tomdale.net/2012/01/amd-is-not-the-answer/
  5. http://requirejs.org/docs/api.html