【发布时间】:2020-06-25 13:07:25
【问题描述】:
可能非常简单,但我不确定如何正确编写以下评估和分配,我正在尝试将配置值分配给小部件并在未设置配置时设置回退默认值。如果我尝试的方式不可能,我可以在设置值之前回退到 if/else。感谢任何指点。
console.log(config);
// Outputs
{autoplay: false}
$(el).slick({
// Sets autoplay to true instead of false, as it evaluates it as not null (I think)
autoplay: config.autoplay || true,
// If i set it this way, it works with the correct value as not being evaluated, but then i have no fallback value
//autoplay: config.autoplay
});
【问题讨论】:
-
如果
config.autoplay ?? true不是一个选项,请尝试(config.hasOwnProperty("autoplay") ? config.autoplay : true)。 -
这能回答你的问题吗? How to set default boolean values in JavaScript? — 在“js 默认布尔值”的第一批 Google 搜索结果中。
标签: javascript boolean