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
343 views
in Technique[技术] by (71.8m points)

javascript - Node.js module.exports的用途是什么,如何使用它?(What is the purpose of Node.js module.exports and how do you use it?)

What is the purpose of Node.js module.exports and how do you use it?

(Node.js module.exports的用途是什么,如何使用它?)

I can't seem to find any information on this, but it appears to be a rather important part of Node.js as I often see it in source code.

(我似乎找不到任何相关信息,但它似乎是Node.js的重要组成部分,正如我在源代码中经常看到的那样。)

According to the Node.js documentation :

(根据Node.js文档 :)

module

(模组)

A reference to the current module .

(对当前module引用。)

In particular module.exports is the same as the exports object.

(特别是module.exports与导出对象相同。)

See src/node.js for more information.

(有关更多信息,请参见src/node.js)

But this doesn't really help.

(但这并没有真正的帮助。)

What exactly does module.exports do, and what would a simple example be?

(module.exports到底是做什么的,一个简单的例子是什么?)

  ask by mrwooster translate from so

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

1 Reply

0 votes
by (71.8m points)

module.exports is the object that's actually returned as the result of a require call.

(module.exports是由于require调用而实际返回的对象。)

The exports variable is initially set to that same object (ie it's a shorthand "alias"), so in the module code you would usually write something like this:

(最初, exports变量设置为相同的对象(即,它是“ alias”的简写),因此在模块代码中,您通常会编写如下代码:)

let myFunc1 = function() { ... };
let myFunc2 = function() { ... };
exports.myFunc1 = myFunc1;
exports.myFunc2 = myFunc2;

to export (or "expose") the internally scoped functions myFunc1 and myFunc2 .

(导出(或“暴露”)内部作用域函数myFunc1myFunc2 。)

And in the calling code you would use:

(在调用代码中,您将使用:)

const m = require('./mymodule');
m.myFunc1();

where the last line shows how the result of require is (usually) just a plain object whose properties may be accessed.

(最后一行显示require的结果(通常)只是一个可以访问其属性的普通对象。)

NB: if you overwrite exports then it will no longer refer to module.exports .

(注意:如果您覆盖exports则它将不再引用module.exports 。)

So if you wish to assign a new object (or a function reference) to exports then you should also assign that new object to module.exports

(因此,如果您希望为exports分配一个新对象(或函数引用),则还应该将该新对象分配给module.exports)


It's worth noting that the name added to the exports object does not have to be the same as the module's internally scoped name for the value that you're adding, so you could have:

(值得注意的是,添加到exports对象的名称不必与要添加的值的模块内部作用域名称相同,因此您可以:)

let myVeryLongInternalName = function() { ... };
exports.shortName = myVeryLongInternalName;
// add other objects, functions, as required

followed by:

(其次是:)

const m = require('./mymodule');
m.shortName(); // invokes module.myVeryLongInternalName

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

...