# AMD模块

* **原文地址：**[**http://www.ruanyifeng.com/blog/2012/11/require\_js.html**](http://www.ruanyifeng.com/blog/2012/11/require_js.html)
* **原文链接：**[**http://www.ruanyifeng.com/blog/2012/10/asynchronous\_module\_definition.html**](http://www.ruanyifeng.com/blog/2012/10/asynchronous_module_definition.html)

## AMD模块:异步加载模块（requireJS）

* **模块名配置**

加载模块的时候，需要用到模块的名称，当模块文件都是在同一目录下，可以把文件名称当做模块名直接加载引用，这些文件必须都是符合AMD规范，即文件里使用define 定义模块过。当然也可以配置非标准的模块，即没有使用define定义。

```javascript
require(["jquery",],function($){

 ......

})
```

再引入模块的时候会首先加载jquery.js和underscore.js，加载完成再执行回调函数。

### **当不在同一路径下的时候就需要配置模块了,使用require.config()方法,写在文件最上面。path用来定义路径**

```javascript
require.config({

    paths: {

    　　　　　　"jquery": "./lib/jquery.min",//路径可以是网络地址 https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min
    　　　　　
    　　　　}


});

require(["jquery"],function($){
....

});
```

### 有很多库是没有使用AMD规范的，加载的时候必须使用shim属性配置，比如underscore.js,其中exports是模块调用时使用的名称。deps这个模块所依赖的其他模块

```javascript
require.config({

shim:{
 underscore：{
  exports: '_'，
  deps:[],
 }
}
path:{



}，


})
```

* **模块的定义：define**

#### AMD是异步加载的模块，通过define来定义模块导出,要是依赖其他的模块，那第一个参数是一个数组，里面是被依赖的模块。math是一个就是math.js路径的简写，define的回调函数里要返回要用的变量。

```javascript
define(["math"],function(math){

    let a = 1;
    let b = function(){

        return 34 ;

    }

   return {
       a:a,
       b:b
   }



});
```

* **模块的加载：require**

**AMD通过require加载模块，**

```javascript
require(["math"],function(){





});
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sekin.gitbook.io/myjs/mo-kuai-hua-bian-cheng/amdmo-kuai.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
