gpt4 book ai didi

javascript - 在模板文字中使用条件

转载 作者:行者123 更新时间:2023-12-03 09:31:45 25 4
gpt4 key购买 nike

我知道有更优雅的方法来定义包含变量的字符串,
但是如果我想在 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 快捷方式?

最佳答案

用这个:

let a = `test${conditional ? a : b} more text`;

关于javascript - 在模板文字中使用条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45676974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com