Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

javascript - Node.js中的module.exports与export(module.exports vs exports in Node.js)

I've found the following contract in a Node.js module:

(我在Node.js模块中找到了以下合同:)

module.exports = exports = nano = function database_module(cfg) {...}

I wonder whats the different between module.exports and exports and why both are used here.

(我不知道什么之间的不同module.exportsexports ,为什么都被用在这里。)

  ask by Andreas K?berle translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Even though question has been answered and accepted long ago, i just want to share my 2 cents:

(即使问题早已得到回答和接受,我也只想分享我的2美分:)

You can imagine that at the very beginning of your file there is something like (just for explanation):

(您可以想象,在文件的开头有类似的内容(仅供说明):)

var module = new Module(...);
var exports = module.exports;

在此处输入图片说明

So whatever you do just keep in mind that module.exports and NOT exports will be returned from your module when you're requiring that module from somewhere else.

(所以,不管你做什么只是记住, module.exports和NOT exports将从您的模块,当你需要从别的地方该模块被退回。)

So when you do something like:

(因此,当您执行以下操作时:)

exports.a = function() {
    console.log("a");
}
exports.b = function() {
    console.log("b");
}

You are adding 2 function a and b to the object on which module.exports points too, so the typeof the returning result will be an object : { a: [Function], b: [Function] }

(要添加2功能ab ,以在其上对象module.exports太点,所以typeof返回的结果将是一个object{ a: [Function], b: [Function] })

Of course, this is the same result you will get if you are using module.exports in this example instead of exports .

(当然,这是同样的结果,如果你使用的是你会得到module.exports在这个例子中,而不是exports 。)

This is the case where you want your module.exports to behave like a container of exported values.

(在这种情况下,您希望您的module.exports表现得像一个导出值的容器。)

Whereas, if you only want to export a constructor function then there is something you should know about using module.exports or exports ;(Remember again that module.exports will be returned when you require something, not export ).

(而如果只想导出构造函数,那么使用module.exportsexports应该有一些知识;(再次记住, module.exports将在需要时返回,而不是export )。)

module.exports = function Something() {
    console.log('bla bla');
}

Now typeof returning result is 'function' and you can require it and immediately invoke like:

(现在, typeof返回的结果是'function' ,您可以要求它并立即调用:)


var x = require('./file1.js')(); because you overwrite the returning result to be a function.

(因为您将返回结果覆盖为一个函数。)

However, using exports you can't use something like:

(但是,使用exports不能使用类似以下内容的东西:)

exports = function Something() {
    console.log('bla bla');
}
var x = require('./file1.js')(); //Error: require is not a function

Because with exports , the reference doesn't point anymore to the object where module.exports points, so there is not a relationship between exports and module.exports anymore.

(因为与exports ,参考已不指向该对象module.exports点,所以没有之间的关系exportsmodule.exports了。)

In this case module.exports still points to the empty object {} which will be returned.

(在这种情况下, module.exports仍指向将返回的空对象{} 。)

Accepted answer from another topic should also help: Does Javascript pass by reference?

(接受的另一个主题的答案也应该有所帮助: Javascript是否通过引用传递?)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...