function getFormatDate(date, type){ if(type === 1) { return '2017-06-15' } if(type === 2) { return '2017年6月15日' } }
a-util.jscss
function aGetFormatDate(date) { return getFormatDate(date, 2); }
a.jshtml
var dt = new Date(); console.log(aGetFormatDate(dt))
index.html前端
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <script src="util.js"></script> <script src="a-util.js"></script> <script src="a.js"></script> </body> </html>
*:node
define(function() { var util = { getFormatDate: function(date, type){ if (type === 1) { return '2017-06-20' } if (type === 2) { return '2017年6月20日' } } } return util; });
a-util.jswebpack
// 依賴於util.js,返回util define(['./util.js'], function(util) { var aUtil = { aGetFormatDate: function(date) { return util.getFormatDate(date,2); } } return aUtil; });
a.jses6
define(['./a-util.js'], function(aUtil) { var a = { printDate: function(date) { console.log(aUtil.aGetFormatDate(date)) } } return a; });
main.jsweb
require(['./a.js'], function(a){ var date = new Date(); a.printDate(date); })
index.htmlnpm
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <p>AMD test</p> <script data-main='./main.js' src="https://cdn.bootcss.com/require.js/2.3.3/require.min.js"></script> </body> </html>
*:前端工程化
module.exports = { getFormatDate: function(date, type){ if (type === 1) { return '2017-06-15' } if (type === 2) { return '2017年6月15日' } } }
a-util.js瀏覽器
var util = require('./util.js'); module.exports = { aGetFormatDate: function(date){ return util.getFormatDate(date, 2); } }