前端require代码抽离小技巧转载

原创
小哥 3年前 (2022-10-18) 阅读数 54 #大杂烩

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

转载于:https://www.cnblogs.com/guangzan/p/11224503.html

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除

热门