前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Node.js Module – exports 和module.exports之间的联系与区别

Node.js Module – exports 和module.exports之间的联系与区别

作者头像
蛋未明
发布2022-05-09 12:39:24
7120
发布2022-05-09 12:39:24
举报
文章被收录于专栏:蛋未明的专栏蛋未明的专栏

来自本人论坛:www.tnodejs.com   tnodejs.com

最近大家都说两者是一样的,其实不然,本文来自

http://www.hacksparrow.com/node-js-exports-vs-module-exports.html 翻译 相信大家都很熟悉 exports 的用法了,你可以创建一个 function 在你的模块中如下:

代码语言:javascript
复制
exports.name = function() {
    console.log('My name is Lemmy Kilmister');
};

我们可以通过如下方法执行访问:

代码语言:javascript
复制
var rocker = require('./rocker.js');
rocker.name(); // 'My name is Lemmy Kilmister'

那么 module.exports 存在的意义是什么?他是否在编码中也是正确合法的? 在开源的 node.js 代码中可以看出, module.exports 才是真正的模块 export ,而 exports 仅仅是 module.exports 的一个帮手。你的所有模块 export 的,最终都是通过 module.exports 返回出去的。 Exports 的作用主要是将所有的属性和方法整理、连接给 module.exports ,当 module.exports 还未执行。如果 module.exports 执行以后,所有的 exports 方法都将失效。 下面的例子就是说明上面一点 创建一个 rocker.js:

代码语言:javascript
复制
module.exports = 'ROCK IT!';
exports.name = function() {
    console.log('My name is Lemmy Kilmister');
};

创建另外一个文件,并且执行:

代码语言:javascript
复制
var rocker = require('./rocker.js');
rocker.name(); // TypeError: Object ROCK IT! has no method 'name'

可以看到执行结果中无法获取 name 这个 function 。 rocker.js 中最开始就执行了 module.exports ,根据之前我们介绍的,在 module.exports 执行后他将拒绝所有的 exports 模块,因此我们的 exports.name 将会失效。从而我们无法在 require 模块后获取该方法。 你可能意识到,你的模块并不总是有“模块实例”。你的模块可能是任何的类型的 JavaScript 对象 boolean, number, date, JSON, string, function, array 等等。你可以通过 module.exports 任何的对象。 If you don't set module.exports to anythingexplicitly, the properties of exports and attached to it and returned. In this case, your module is a class:

代码语言:javascript
复制
module.exports = function(name, age) {
    this.name = name;
    this.age = age;
    this.about = function() {
        console.log(this.name +' is '+ this.age +' years old');
    };
};

and you'd use it this way:

代码语言:javascript
复制
var Rocker = require('./rocker.js');
var r = new Rocker('Ozzy', 62);
r.about(); // Ozzy is 62 years old

In this case, your module is an array:

代码语言:javascript
复制
module.exports = ['Lemmy Kilmister', 'Ozzy Osbourne', 'Ronnie James Dio', 'Steven Tyler', 'Mick Jagger'];

and you may use it this way:

代码语言:javascript
复制
var rocker = require('./rocker.js');
console.log('Rockin in heaven: ' + rocker[2]); //Rockin in heaven: Ronnie James Dio

So you get the point now - if you want yourmodule to be of a specific object type, use module.exports; if you want yourmodule to be a typical module instance, use exports. The result of attaching properties tomodule.exports is akin to attaching properties to exports. For example this:

代码语言:javascript
复制
module.exports.name = function() {
    console.log('My name is Lemmy Kilmister');
};

does the same thing as:

代码语言:javascript
复制
exports.name = function() {
    console.log('My name is Lemmy Kilmister');
};

But note that, they are not the same thing.As I said earlier module.exports is the real deal, exports is just its littlehelper. Having said that, exports is the recommended object unless you areplanning to change the object type of your module from the traditional 'module instance'to something else. I hope this post helped you understand thedifference between exports and module.exports, and learn a bit more about howmodules work in Node.js. Any questions, ping me in the comments. 加上官网的介绍

The exports object is created by the Modulesystem. Sometimes this is not acceptable, many want their module to be aninstance of some class. To do this assign the desired export object to module.exports. For example suppose we were making amodule called a.js

代码语言:javascript
复制
var EventEmitter = require('events').EventEmitter;
module.exports = new EventEmitter();
// Do some work, and after some time emit
// the 'ready' event from the module itself.
setTimeout(function() {
  module.exports.emit('ready');
}, 1000);

Then in another file we could do

代码语言:javascript
复制
var a = require('./a');
a.on('ready', function() {
  console.log('module a is ready');
});

Note that assignment to [font='Lucida Console']module.exports must be done immediately. It cannotbe done in any callbacks. This does not work:

x.js:

代码语言:javascript
复制
setTimeout(function() {
  module.exports = { a: "hello" };
}, 0);

y.js:

代码语言:javascript
复制
var x = require('./x');
console.log(x.a);
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2012-06-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档