我正在阅读 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/