node.js module.exports & exports & module.export all in one

cjs

const log = console.log;

log(`exports`, exports);
log(`module`, module); // TypeError: Cannot set property 'a' of undefined
// module.export.a = 1;
// module.export.b = 2;
// module.export.c = 3;
// module.export.d = 4; // 自定义属性 umd
module.umd = {
a: 1,
b: 2,
c: 3,
d: 4,
};
// 自定义属性 export
module.export = {
a: 1,
b: 2,
c: 3,
d: 4,
}; exports.a = 11;
exports.b = 22;
exports.c = 33;
exports.d = 44; // module.exports 覆盖 exports
module.exports = { c: 333 };
module.exports.d = 444; log(`\nexports`, exports);
log(`module`, module); /* exports { a: 11, b: 22, c: 33, d: 44 }
module Module {
id: '.',
path: '/Users/xgqfrms-mbp/Documents/GitHub/umd-npm-package/src',
exports: { c: 333, d: 444 },
parent: null,
filename: '/Users/xgqfrms-mbp/Documents/GitHub/umd-npm-package/src/export.js',
loaded: false,
children: [],
paths: [
'/Users/xgqfrms-mbp/Documents/GitHub/umd-npm-package/src/node_modules',
'/Users/xgqfrms-mbp/Documents/GitHub/umd-npm-package/node_modules',
'/Users/xgqfrms-mbp/Documents/GitHub/node_modules',
'/Users/xgqfrms-mbp/Documents/node_modules',
'/Users/xgqfrms-mbp/node_modules',
'/Users/node_modules',
'/node_modules'
],
umd: { a: 1, b: 2, c: 3, d: 4 },
export: { a: 1, b: 2, c: 3, d: 4 }
}
*/

module.exports vs exports

const log = console.log;

const app = (datas = [], debug = false) => {
log(`args =`, datas);
}; const test = `abc`; // default export (const app = require(`./app`);)
module.exports = app; module.exports.test = test; //
// module.exports.app = app; //
// module.exports = {
// app,
// }; log(`exports =`, exports);
log(`module.exports =`, module.exports);
// exports = {}
// module.exports = [Function: app] { test: 'abc' }
// log(`module`, module);
const log = console.log;

//  import default module
const app = require(`./app`);
// const app, {test} = require(`./app`); log(`\napp =`, app, app.app);
app();
// app = [Function: app] undefined
// args = [] log(`test =`, app.test);
// test = abc
const log = console.log;

const app = (datas = [], debug = false) => {
log(`args =`, datas);
}; const test = `abc`; //
// exports = app; // module export (const { app, } = require(`./app`);)
exports.app = app; exports.test = test; log(`exports =`, exports);
log(`module.exports =`, module.exports);
// exports = { app: [Function: app], test: 'abc' }
// module.exports = { app: [Function: app], test: 'abc' }
// log(`module`, module);
const log = console.log;

//  imports multi modules
const { app, test, } = require(`./app`); log(`\napp =`, app);
app();
// app = [Function: app]
// args = [] log(`test =`, test);
// test = abc

exports = module.exports

module.exports 与 exports 指向同一个Object 引用

https://blog.tableflip.io/the-difference-between-module-exports-and-exports

"use strict";

/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-10-01
* @modified
*
* @description
* @difficulty Easy Medium Hard
* @complexity O(n)
* @augments
* @example
* @link
* @solutions
*
* @best_solutions
*
*/ const log = console.log; // module.exports 与 exports 指向同一个Object 引用 // SyntaxError: Identifier 'module' has already been declared
// module = {
// exports: {},
// //others
// };
// // {exports: {…}} // module.exports;
// // {} // exports = module.exports;
// // {} // exports.a = `a`; // module.exports.a = `aa`;
// module.exports.b = `b`; // log(`exports =`, exports);
// log(`module.exports =`, module.exports); /* exports = { a: 'aa', b: 'b' }
module.exports = { a: 'aa', b: 'b' } */ const test = () => {
const module = {
exports: {},
//others
};
// {exports: {…}}
module.exports;
// {}
const exports = module.exports;
// {}
exports.a = `a`;
module.exports.a = `aa`;
module.exports.b = `b`;
log(`exports =`, exports);
log(`module.exports =`, module.exports);
}
test(); /* exports = { a: 'aa', b: 'b' }
module.exports = { a: 'aa', b: 'b' } */

refs

https://gist.github.com/xgqfrms/b69677e524d4c4e154fef6342914eb00



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


最新文章

  1. 关于DOM树的常见增删操作
  2. 网络安全之PHP安全编程建议
  3. struct inode 和 struct file
  4. iOS应用中URL地址如何重定向-b
  5. sharepoint 模糊搜索
  6. poi读写Excel文件
  7. sql发邮件
  8. 部署到Linux使用VS Code 开发.NET Core 应用程序
  9. Linq无聊练习系列3--聚合函数练习
  10. java 利用反射构造泛型类
  11. 【转】scatterlist && DMA
  12. MyBatis物理分页的代码实现
  13. MFC关于多线程中传递窗口类指针时ASSERT_VALID出错的另类解决 转
  14. 干货!Android Studio快捷键VS Eclipse快捷键~亲测!
  15. 【C/C++】递归算法
  16. MFC通过button控制编辑框是否显示系统时间
  17. mysql 地理位置定位
  18. iscsi target 研究
  19. Unity3D学习笔记(一):Unity简介、游戏物体、组件和生命周期函数
  20. 为何 Delphi的 Local Variables 突然没有值显示了

热门文章

  1. Windows 2008server部署pxe启动安装windows系统
  2. virtualenv安装和配置
  3. 数据湖-Apache Hudi
  4. libuv线程通信
  5. pycharm创建文件夹以及查看源文件存放位置(FOR MAC)
  6. 「NOIP2009」最优贸易
  7. 引入 Gateway 网关,这些坑一定要学会避开!!!
  8. Java基本类型的内存分配在栈还是堆
  9. Java程序操作Hive
  10. P3384 [模板] 树链剖分