在Github上看到一个js的插件,于是兴起自己也来尝试坐下...于是参考GIthub上的一个项目自己也做个简单的实验。
可以先参考别人项目的组织及结构来开发代码Github上看到的js插件的代码格式一般是这样的:
(function(){
var MyApp = {}; //模块名
/*
* 模块其他成员
*/
//如
var Component = {} ;
(function(){
Component.xxx = function(){
// do something
}
});
MyApp.Component = Component ;
// 使用CommonJs模块类型
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = MyApp;
}
exports.MyApp = MyApp;
}
//AMD模块
if (typeof define === 'function' && define.amd) {
define('MyApp', [], function () {
return MyApp;
});
}
//浏览器
if (typeof window === 'object' && typeof window.document === 'object') {
window.MyApp = MyApp;
}
})