javascript - NodeJS 中的 varexports = module.exports = {}

标签 javascript node.js

我从之前的帖子和文章中了解到,exports 对象位于 global 对象上。我遇到了这段令我困惑的代码:

let blue = 'blue'
let red = 'red'

var exports = module.exports = {
    red,
    blue
};

此代码将 module.exports 设置为名为 exports 的变量,然后将其设置为要导出的对象。

但是我对这种语法感到困惑:

示例1:

var exports = module.exports = {}

这到底是如何工作的?因为通常在 JS 中你不能对一个变量赋值两次。例如,这会产生错误:

示例2:

let foo = 5 = 4;

为什么示例 1 中的代码没有错误,而示例 2 中的代码却错误?

最佳答案

let foo = 5 = 4;

因为它是从右向左解析的:

let foo = (5 = 4);

并且 5 不是变量,因此您无法为其分配内容。但是它可以与标识符一起使用:

let bar;
let foo = bar = 5;

关于javascript - NodeJS 中的 varexports = module.exports = {},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47609810/

相关文章:

重复字符的 JavaScript 正则表达式规则

javascript - 使用 onkeypress 事件验证两个输入字段

javascript - iframe 上的链接在 IFrame 中创建页面的另一个 View

html - 来自字符串格式问题的 Jade HTML

javascript - 断言selenium webdriver Node js

node.js - 带 Handlebars 模板引擎的 Sails.js

javascript - 如果页面上有某个元素,则只显示某个 <div>

javascript - html中的脚本标签放在哪里?

javascript - mocha.opts 文件中的 --reporter 规范是什么?

javascript - 将 64 位 Steam ID 转换为 32 位帐户 ID