跳到主要内容
CNode

一句话说清exports和module.exports的区别

社区
Sstepjacky__legacy_530e086f3c90760f0d01d5f8发布于 12 年前最后回复 12 年前
672050

无需多言,不明白这两者区别的大多没看官方解释,只有一句话 If you want the root of your module's export to be a function (such as a constructor) or if you want to export a complete object in one assignment instead of building it one property at a time, assign it to module.exports instead of exports.

大家明白了吧

查看回复

回复 (6)

B
blacktea#612 年前

其实懂引用类型和值类型的都会很容易理解吧。。

A
atian25#412 年前
var module = {
  exports: {}
};

function your_module_define(require, exports, module){
 //修改入参的引用, 不会影响外部变量
 exports = {
   nouse: null
 };
 //修改对象的引用才有用
 module.exports = {}
}
A
atian25#312 年前

很简单, exports是入参, 外部传入的是module.exports, 而修改入参的引用并不会影响外部对象

T
think2011#212 年前

不难理解

H
hackerjs#112 年前

//个人理解 //node.js 默认声明 //var exports = {}; //module.exports = exports;

moudle.exports = {a:1}; exports.a = 2;

//导出的是{a:1}

参与回复

登录后即可参与回复。登录