Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

javascript - CommonJs模块系统中“ module.exports”和“ exports”之间的区别(Difference between “module.exports” and “exports” in the CommonJs Module System)

On this page ( http://docs.nodejitsu.com/articles/getting-started/what-is-require ), it states that "If you want to set the exports object to a function or a new object, you have to use the module.exports object."

(在此页面( http://docs.nodejitsu.com/articles/getting-started/what-is-require )上,声明“如果要将导出对象设置为函数或新对象,则必须使用module.exports对象。”)

My question is why.

(我的问题是为什么。)

// right
module.exports = function () {
  console.log("hello world")
}
// wrong
exports = function () {
  console.log("hello world")
}

I console.logged the result ( result=require(example.js) ) and the first one is [Function] the second one is {} .

(我console.logged结果( result=require(example.js) ),第一个是[Function]第二个是{} 。)

Could you please explain the reason behind it?

(您能否解释其背后的原因?)

I read the post here: module.exports vs exports in Node.js .

(我在这里阅读了这篇文章: Node.js中的module.exports与export 。)

It is helpful, but does not explain the reason why it is designed in that way.

(它很有帮助,但没有解释以这种方式设计它的原因。)

Will there be a problem if the reference of exports be returned directly?

(如果直接返回出口参考书会不会有问题?)

  ask by Xiao Peng - ZenUML.com translate from so

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

1 Answer

0 votes
by (71.8m points)

module is a plain JavaScript object with an exports property.

(module是一个带有exports属性的普通JavaScript对象。)

exports is a plain JavaScript variable that happens to be set to module.exports .

(exports是一个普通的JavaScript变量,恰好被设置为module.exports 。)

At the end of your file, node.js will basically 'return' module.exports to the require function.

(在文件末尾,node.js基本上将“返回” module.exportsrequire函数。)

A simplified way to view a JS file in Node could be this:

(在Node中查看JS文件的一种简化方法是:)

var module = { exports: {} };
var exports = module.exports;

// your code

return module.exports;

If you set a property on exports , like exports.a = 9;

(如果您为exports设置属性,例如exports.a = 9;)

, that will set module.exports.a as well because objects are passed around as references in JavaScript, which means that if you set multiple variables to the same object, they are all the same object;

(,这也会设置module.exports.a ,因为对象是作为JavaScript中的引用传递的,这意味着,如果将多个变量设置到同一对象,则它们都是同一对象;)

so then exports and module.exports are the same object.

(所以后来exportsmodule.exports是同一个对象。)


But if you set exports to something new, it will no longer be set to module.exports , so exports and module.exports are no longer the same object.

(但是,如果你设置exports新的东西,它将不再被设为module.exports ,因此exportsmodule.exports不再是同一个对象。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...