十二、模块化

2019-02-16  本文已影响0人  懒羊羊3号

1、es6

export { a, b }  对应 import { a, b } from 'xxx'
export default name 对应 import name from 'xxx'

2、闭包模块化

var module = (function() {
    var N = 5;
    function print(x) {
        console.log("The result is: " + x);
    }
    function add(a) {
        var x = a + N;
        print(x);
    }
    return {
        description: "This is description",
        add: add
    };
})();
console.log(module.description); // 输出"this is description" 
module.add(5); // 输出“The result is: 10”
let langUtil = {};
(function ($) {
  $.getLang = function (lang, pageFlag) {
    let langPackage;
    ...
    return langPackage;
  };
})(langUtil);
module.exports = langUtil;

//  获取
const langUtil = require('../lang/util');
const lang = langUtil.getLang(req.query.lang, productType);
上一篇 下一篇

猜你喜欢

热点阅读