module.export和export有什么不同?
module.export
和export
什么区别?
如果module.export对象中有一些属性会怎么样?那么export.xx
会失效吗?
首先是exports
和module.exports
而不是export
和module.export
(在JavaScript中也有一个export
关键字,但在Node中不支持)。
每个Node模块都包含这个函数:
(function (exports, require, module, __filename, __dirname) { // Your module code actually lives in here });
请参阅: https : //nodejs.org/api/modules.html#modules_the_module_wrapper
有些人认为你的模块不会像这个函数那样返回导出的对象。 它只能通过向提供的参数添加属性来将数据传递给调用者。
module.exports
和exports
最初是同一个对象 – 但是它是module.exports
,如果它们最终不是同一个对象,它实际上会被导出 。
这意味着这将工作相同:
module.exports.x = 1; # or: exports.x = 1;
因为它改变了同一个对象的属性。
但是这不会是一样的:
module.exports = {x: 1};
如下所示:
exports = {x: 1};
最后一个将不会导出 x
因为它将replace 导出中的对象(原来是与 module.exports
相同的对象),而将module.exports
的默认空对象实际导出。
module.exports = {x: 1};
也替代一个新的对象,但它把它需要的地方实际上出口。 请注意,这条指令changes the property
module
对象changes the property
,在你的模块封装的隐式函数返回之后,调用者可以看到它。
现在这将工作:
exports = {x: 1}; module.exports = exports;
因为它用一个新的replaceexports
的对象,但它也把它放入module.exports
。
请记住,如果你没有用一个新的对象replace其中的一个,那么总是module.exports
重要的是module.exports
,然后在设置它们的属性时可以互换使用它们:
exports.x = 1; module.exports.y = 2;
有关更多信息,请参阅此答案:
- 为什么我们在nodejs中使用导出?
模块是对象, 出口是模块对象的属性(variables)。
const module = { //property exports: {} };
导出是module.exports的快捷方式,直到它被分配了新的对象
-
导出=== module.exports
module.exports.hello = true; exports.hello = true;
-
出口!== module.exports
module.exports.hello = true; exports = { hello: true };
出口物业完全被新物件取代。
参考
function require(/* ... */) { const module = { exports: {} }; ((module, exports) => { // Your module code here. In this example, define a function. function someFunc() {} exports = someFunc; // At this point, exports is no longer a shortcut to // module.exports, and // this module will still export an empty default object. module.exports = someFunc; // At this point, the module will now export someFunc, instead of //the default object. })(module, module.exports); return module.exports; }
module.exports
实际上是require
函数返回的内容,当你传递你的脚本的path来需要函数的时候,你的代码将会包装并运行在一个名为exports
的参数的函数expression式中,这个参数是一个空的对象,然后module.exports
会传递给它。 通常它是module.exports
的缩写,换句话说,它是两个指向内存中相同对象的variables,所以它看起来相似,对吗?
但其实有一个区别, exports
在某些模式上有所不同,让我们来举一个关于这些模式的例子,比如说我们有一个名为tada.js的脚本:
exports = function(){ console.log('Tada'); } console.log(exports); console.log(module.exports);
如果我需要这个脚本在我的app.js中:
var tada = require('./tada');
那么你期望看到什么?
好吧,这将是输出:
console.log(exports); // [Function] console.log(module.exports); // {}
它显示exports
作为一个函数和module.exports
作为一个空的对象。 但是,正如我告诉过你的,这两个variables只是指向内存中同一个对象的两个variables,通过引用传递,那么它们是不是应该具有相同的值? 好吧,也许用任何其他语言,它会这样做,但在JavaScript中有些不同。
在JavaScript中,如果我们使用=
来分配一个值,它实际上是在内存中创build一个新的对象,所以换句话说,在我们的例子中, exports
和module.exports
之间的reference
将被打破,意味着exports
和module.exports
指向记忆中的同一个物体,但是当我们这样做的时候:
exports = function(){ console.log('Tada'); }
那个引用断了,现在exports
是指向一个新的内存对象。
也请记住,要求函数只返回module.exports
,不exports
,这就是为什么我们在我们的例子中为module.exports
了空对象,并且由于引用中断, module.exports
, exports
不再指向同一个对象。
要解决这个问题,你需要mutate
而不是覆盖。 所以如果我们把我们的例子改为这个,那么exports
和module.exports
将具有相同的值:
exports.tada = function(){ console.log('Tada'); } console.log(exports); console.log(module.exports);