我试图用这个条件语句来缩短我的代码
我有
if(title =='project'){
title = text + ': ' + this.projectTitle;
}else{
title = this.projectTitle;
}
我觉得有更好的方法来重写这个。有谁知道该怎么做?
谢谢!
我试图用这个条件语句来缩短我的代码
我有
if(title =='project'){
title = text + ': ' + this.projectTitle;
}else{
title = this.projectTitle;
}
我觉得有更好的方法来重写这个。有谁知道该怎么做?
谢谢!
尝试三元:
title = (title =='project') ? text + ': ' + this.projectTitle: this.projectTitle;
if
再现您的/的三元组else
:
title = title == 'project' ? text + ': ' + this.projectTitle : this.projectTitle;
虽然如果你真的想缩短它:
title = (title == 'project' ? text + ': ' : '') + this.projectTitle;
参考:
简单的内联条件;
title = ((title =='project') ? text + ': ' + this.projectTitle : this.projectTitle);
查看链接了解更多详情
title = (title == 'project' ? text + ': ' : '') + this.projectTitle;
var title = (title =='project' ? text + ': ' + this.projectTitle : this.projectTitle);