javascript - JavaScript 评估速记问题
问题描述
可能非常简单,但我不确定如何正确编写以下评估和分配,我正在尝试将配置值分配给小部件并在未设置配置时设置回退默认值。如果我尝试的方式不可能,我可以在设置值之前回退到 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
});
解决方案
$(el).slick({
autoplay: config.autoplay != null ? config.autoplay : true
});
这将检查值是否既不是null
也不是undefined
。
由于自动播放是假的,false || true
总是会导致真。
推荐阅读
- ios - 在 RxSwift 中点击按钮不会在平面图范围内调用 Moya 网络请求
- reactjs - 如何重构常见的 React 组件?
- elasticsearch - Elasticsearch:刚过滤时,为什么要使用过滤后的查询类型
- c# - 使用实体框架从有条件的数据库中选择不同的记录
- laravel - 在每个控制器请求之前执行函数
- javascript - 从多选选项列表创建对象并输入
- javascript - 在 component.EXTJs 中动态添加时,Ext.XTemplate() 不呈现
- json - Angular 6 返回 JSON 结果而不是 HTML 模板
- macos - Outlook-addin html body 对 Outlook for mac 16.24 的支持
- yocto - Yocto:没有提供 python-re-native