【问题标题】:Difference between const {Hoge} and const Hoge (node.js)const {Hoge} 和 const Hoge (node.js) 之间的区别
【发布时间】:2020-12-23 13:37:31
【问题描述】:

我不明白下面代码的区别。有什么区别?

答:

const Hoge = require('@foo/hoge');

乙:

const {Hoge} = require('@foo/hoge');

谢谢

【问题讨论】:

标签: node.js


【解决方案1】:

在第一个示例中,Hoge 被分配给模块中的 module.exports

// @foo/hoge entry file
module.exports = {
    Hoge: 123
}

// Your file
const Hoge = require("@foo/hoge")
// Hoge = { Hoge: 123 }

在第二个示例中,您将属性 Hoge 从对象 module.exports 中取出

// Your file
const { Hoge } = require("@foo/hoge")
// Hoge = 123

【讨论】:

  • 感谢您的回答!我理解这种行为。在文档中哪里可以找到这个?很难找到符号 ({}) 的行为差异...
猜你喜欢
  • 2018-11-05
  • 2016-08-02
  • 2013-04-23
  • 2013-07-21
  • 2011-02-25
  • 1970-01-01
  • 1970-01-01
  • 2012-12-16
相关资源
最近更新 更多