Home  >  Article  >  Web Front-end  >  what is javascript adm

what is javascript adm

青灯夜游
青灯夜游Original
2021-06-09 12:07:292261browse

In JavaScript, AMD refers to "asynchronous module definition", which is a module specification. It loads modules asynchronously. The loading of the module does not affect the running of subsequent statements; all statements that rely on this module , are defined in a callback function. This callback function will not run until the loading is completed.

what is javascript adm

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

As websites gradually become "Internet applications", the Javascript codes embedded in web pages are becoming larger and more complex.

Web pages are becoming more and more like desktop programs, requiring a team's division of labor and collaboration, progress management, unit testing, etc... Developers have to use software engineering methods to manage the business logic of web pages.

Javascript modular programming has become an urgent need. Ideally, developers only need to implement the core business logic, and other modules can be loaded by others.

However, Javascript is not a modular programming language. It does not support "classes", let alone "modules". (The sixth edition of the ECMAScript standard, which is being formulated, will officially support "classes" and "modules", but it will take a long time to be put into practical use.)

The Javascript community has made a lot of efforts to improve the existing In the running environment, the effect of "module" is achieved. This article summarizes the current best practices of "Javascript modular programming" and explains how to put them into practice. Although this is not an introductory tutorial, you can understand it as long as you have a little understanding of the basic syntax of Javascript.

Module specifications

First think about it, why are modules important?

Because of the modules, we can use other people's code more conveniently, and load whatever modules we want for whatever functions we want.

However, there is a prerequisite for this, that is, everyone must write the module in the same way, otherwise you have your way of writing, and I have my way of writing, wouldn't it be a mess! This is even more important considering that there is no official specification for Javascript modules yet.

Currently, there are two popular Javascript module specifications: CommonJS and AMD.

CommonJS

In 2009, American programmer Ryan Dahl created the node.js project to use the JavaScript language for server-side programming.

what is javascript adm

This marks the official birth of "Javascript modular programming". Because to be honest, in a browser environment, not having modules is not a big problem. After all, the complexity of web programs is limited; but on the server side, there must be modules to interact with the operating system and other applications, otherwise there is no way. programming.

The module system of node.js is implemented with reference to the CommonJS specification. In CommonJS, there is a global method require() for loading modules. Assuming there is a math module math.js, it can be loaded as follows.

var math = require('math');

Then, you can call the method provided by the module:

var math = require('math');
math.add(2,3); // 5

Because this series is mainly aimed at browser programming and does not involve node.js, there is no more introduction to CommonJS. All we need to know here is that require() is used to load modules.

Browser environment

After having the server-side module, it is natural that everyone wants the client-side module. And it is best if the two are compatible, so that a module can run on both the server and the browser without modification.

However, due to a major limitation, the CommonJS specification is not suitable for browser environments. The code in the previous section will have a big problem if it is run in a browser. Can you see it?

var math = require('math');
math.add(2, 3);

The second line of math.add(2, 3) runs after the first line of require('math'), so you must wait until math.js is loaded. That is, if it takes a long time to load, the entire app just hangs there waiting.

This is not a problem on the server side, because all modules are stored in the local hard disk and can be loaded synchronously. The waiting time is the reading time of the hard disk. However, for browsers, this is a big problem, because the modules are placed on the server side, and the waiting time depends on the speed of the network. It may take a long time, and the browser is in a "suspended" state.

Therefore, browser-side modules cannot use "synchronous loading" (synchronous), but can only use "asynchronous loading" (asynchronous). This is the background for the birth of the AMD specification.

AMD specification

AMD is the abbreviation of "Asynchronous Module Definition", which means "asynchronous module definition". It loads modules asynchronously, and the loading of the module does not affect the execution of subsequent statements. All statements that depend on this module are defined in a callback function. This callback function will not run until the loading is completed.

AMD也采用require()语句加载模块,但是不同于CommonJS,它要求两个参数:

require([module], callback);

第一个参数[module],是一个数组,里面的成员就是要加载的模块;第二个参数callback,则是加载成功之后的回调函数。如果将前面的代码改写成AMD形式,就是下面这样:

  require(['math'], function (math) {
    math.add(2, 3);
  });

math.add()与math模块加载不是同步的,浏览器不会发生假死。所以很显然,AMD比较适合浏览器环境。

目前,主要有两个Javascript库实现了AMD规范:require.js和curl.js。本系列的第三部分,将通过介绍require.js,进一步讲解AMD的用法,以及如何将模块化编程投入实战。

更多编程相关知识,请访问:编程视频!!

The above is the detailed content of what is javascript adm. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn