-6

我试图用这个条件语句来缩短我的代码

我有

  if(title =='project'){
      title = text + ': ' + this.projectTitle;
  }else{
      title = this.projectTitle;
  }

我觉得有更好的方法来重写这个。有谁知道该怎么做?

谢谢!

4

5 回答 5

1

尝试三元

title = (title =='project') ? text + ': ' + this.projectTitle: this.projectTitle;
于 2013-09-17T17:01:27.043 回答
1

if再现您的/的三元组else

title = title == 'project' ? text + ': ' + this.projectTitle : this.projectTitle;

虽然如果你真的想缩短它:

title = (title == 'project' ? text + ': ' : '') + this.projectTitle;

参考:

于 2013-09-17T17:01:49.967 回答
1

简单的内联条件;

   title =  ((title =='project') ? text + ': ' + this.projectTitle : this.projectTitle);

查看链接了解更多详情

于 2013-09-17T17:02:25.567 回答
1
title = (title == 'project' ? text + ': ' : '') + this.projectTitle;
于 2013-09-17T17:04:51.350 回答
1
var title = (title =='project' ? text + ': ' + this.projectTitle : this.projectTitle);
于 2013-09-17T17:05:05.310 回答