前端require代码抽离小技巧

DEMO

文件目录结构

plugin.js

// /CommonJS规范
// var exports = module.exports;
exports.test = function () {
    console.log("This is a plugin");
}

req1.js

const plugin = require("./plugin");

require("./req2")(plugin); // 等于是执行module.exports

req2.js

module.exports = plugin => {
    // Do something with plugin
    plugin.test();
}

运行req1.js

猜你喜欢

转载自www.cnblogs.com/guangzan/p/11224503.html
今日推荐