---
title: "js中如何封装模块"
author: "sailinglee"
date: "2014-06-03T12:44:59Z"
tags: []
categories: ["Web前端"]
slug: "js"
draft: false
lastmod: "2014-06-03T12:49:17Z"
id: 2075459
url: "https://sailinglee.iteye.com/blog/2075459"
---

统一模块定义：![酷](http://www.iteye.com/javascripts/tinymce/plugins/emotions/img/smiley-cool.gif "酷")

 

```
function UMD(name, context, definition) {
  if (typeof module != 'undefined' && module.exports) module.exports = definition()
  else if (typeof define == 'function' && define.amd) define(definition)
  else context[name] = definition()
}
```

 怎么用？

```
UMD('yourmodule',this,function(args){
    //here is yourmodule code
    var module = ......
    ......

    return module;
})
```

 

参考：[http://www.cnblogs.com/snandy/archive/2012/03/19/2406596.html](http://www.cnblogs.com/snandy/archive/2012/03/19/2406596.html)