【问题标题】:How does module.exports object work without keys? [duplicate]module.exports 对象如何在没有键的情况下工作? [复制]
【发布时间】:2020-03-17 01:50:38
【问题描述】:

module.exports 如何处理没有键的对象?例如,如果我在test.js

const two = 2;
const three = 3;
module.exports = {two, three};    // Is the right-hand side an object? If so, where are its keys?
                                  // It doesn't look like it's object destructuring.

app.js

const test = require("./test");
console.log(test.two);            // 2
console.log(test.three);          // 3

【问题讨论】:

  • 这不是 module.exports 所特有的——这只是 ES6 中引入的对象创建的简写

标签: javascript node.js node-modules


【解决方案1】:

这个:

module.exports = {two, three};

是以下的简写符号:

module.exports = {two: two, three: three};

两者都生成完全相同的对象。这与module.exports 无关。这只是 ES6 object initializer 的简写方式,用于声明您希望属性名称与变量名称相同的对象。

【讨论】:

    猜你喜欢
    • 2011-05-06
    • 2020-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-30
    • 2018-12-09
    • 1970-01-01
    • 2020-10-19
    相关资源
    最近更新 更多