javascript - 无法理解这段关于 ES6 解构的片段

标签 javascript ecmascript-6

我正在阅读 latest one of You Don't Know JS series当它进入解构部分时完全迷失了。请帮助我理解这里的片段。此处的上下文即将使一些指定的配置生效,而其他默认配置仍然可用。

默认值:

var defaults = {
    options: {
        remove: true,
        enable: false,
        instance: {}
    },
    log: {
        warn: true,
        error: true
    }
};

配置:

var config = {
    options: {
        remove: false,
        instance: null
    }
};

作者是如何完成的

config.options = config.options || {};
config.log = config.log || {};
({
    options: {
        remove: config.options.remove = defaults.options.remove,
        enable: config.options.enable = defaults.options.enable,
        instance: config.options.instance =
                      defaults.options.instance
    } = {},
    log: {
        warn: config.log.warn = defaults.log.warn,
        error: config.log.error = defaults.log.error
    } = {}
} = config);

作者对代码段作了这样的描述:

The previous snippet’s approach works because I’m hacking the destructuring and defaults mechanism to do the property === undefined checks and assignment decisions for me. It’s a trick in that I’m destructuring config (see the = config at the end of the snippet), but I’m reassigning all the destructured values right back into config, with the config.options.enable assignment references.

最困惑的是最后一句:with the config.options.enable assignment references. config.options.enable 和config.options 的其他属性有什么区别?

你能为我解释一下代码和描述吗?谢谢!

最佳答案

这是我的错别字。我刚刚在第二版中提出了一个问题来解决它。我应该说 config.enable.XYZ 以明确我指的是所有这些,而不仅仅是那个。很抱歉我的错误给您造成了困惑。

关于javascript - 无法理解这段关于 ES6 解构的片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35944851/

相关文章:

javascript - 如何从 input=File 更改 javascript 中的文件名

javascript - 如何从@Html.Raw(Json.Encode(Model)) 中删除 ESLint 解析错误

javascript - jquery 检测文本框值更改不基于用户输入

javascript - ES6 如何在另一个上下文中的类中获取 _this

javascript - 为什么 ng-repeat 在未被调用时起作用?

javascript - Bootstrap AngularJS 模式正确重定向用户,但页面看起来黑了

javascript - 使用 JavaScript 遍历数据表中的行

javascript - es6过滤并找到相同的对象项

javascript - Node 中并发事件 ('map-reduce' ) 的惯用同步?

javascript - 将数据从 React 传递到 Node Express 服务器