Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module.exports和exports的区别 #15

Open
Clearives opened this issue Apr 26, 2019 · 0 comments
Open

module.exports和exports的区别 #15

Clearives opened this issue Apr 26, 2019 · 0 comments
Labels

Comments

@Clearives
Copy link
Owner

module.exports和exports的区别

Node模块允许你从被引入文件中选择要暴露给程序的函数和变量。如果模块返回的函数或变 量不止一个,那它可以通过设定exports对象的属性来指明它们。但如果模块只返回一个函数或变量,则可以设定module.exports属性。

module和exports

// demo.js
console.log(module)
console.log(exports)

// 输出
Module {
  id: '.',
  exports: {},
  parent: null,
  filename:
   '.../demo1.js',
  loaded: false,
  children: [],
  paths:
   [ ... ] }
{}

module.exports和exports一开始都是一个空对象{},本质上这两个指向的是同一块内存,

require

require引入的对象本质上是module.exports。这就产生了一个问题,当 module.exports和exports指向的不是同一块内存时,exports的内容就会失效。

最终在程序里导出的是module.exports。exports只是对module.exports的一个全 局引用,最初被定义为一个可以添加属性的空对象。如果把exports设定为别的,就打破了module.exports和exports之间的 引用关系。可是因为真正导出的是module.exports,那样exports就不能用了,因为 它不再指向module.exports了。如果你想维持那个链接,可以像下面这样让 module.exports再次引用exports。

// person.js
module.exports = {name: 'clearives'}
exports = {name: 'Mr、z'}


// demo.js

const person = require('./person')
console.log(person) // {name: 'clearives'}


module.exports = exports = {...};

参考:
Node.js实战

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant