【发布时间】:2017-08-14 14:35:22
【问题描述】:
我知道有更优雅的方法来定义包含变量的字符串, 但如果我想在 pre ES6 中添加条件,我会这样做..
var a = "text"+(conditional?a:b)+" more text"
现在我会使用模板文字..
let a;
if(conditional) a = `test${a} more text`;
else a = `test${b} more text`;
有没有更优雅的方式来实现这个条件?是否可以包含 if 快捷方式?
【问题讨论】:
-
我想知道为什么你以前没有写过
var a; if (conditional) a = "text"+a+" more text"; else a = "text"+b+" more text";。当然你仍然可以在 ES6 中使用三元运算符!