使用 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

posted @ 2019-07-22 10:47  guangzan  阅读(260)  评论(0编辑  收藏  举报