CNode

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

Sstepjacky__legacy_530e086f3c90760f0d01d5f8发布于12 年前最后回复12 年前6 回复7180 浏览0 收藏

无需多言,不明白这两者区别的大多没看官方解释,只有一句话 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)

H
hackerjs#1·12 年前

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

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

//导出的是{a:1}

T
think2011#2·12 年前

不难理解

A
atian25#3·12 年前

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

A
atian25#4·12 年前
var module = {
  exports: {}
};

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

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

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