0

我在 js 函数中有一个 js 代码......这包含 if else 条件......它包含相同的功能,唯一的事情是参数更改......

所以唯一不同的是你传递给函数的参数字符串......使用 if/else 调用函数,其余逻辑是相同的......

是否可以调用该函数,其余逻辑相同......

return $(this).each(function () {
                if (coffeeId == "showCoffeeId") {
                    var todayDate = NoteWorklist.getDateTime("appleTime");
                    value.Year = todayDate.getFullYear();
                    value.Month = todayDate.getMonth() + 1;
                    value.Day = todayDate.getDate();
                    value.today = todayDate;
                    value.inputDate = todayDate;
                } else {
                    var todayDate = NoteWorklist.getDateTime("orangeTime");
                    value.Year = todayDate.getFullYear();
                    value.Month = todayDate.getMonth() + 1;
                    value.Day = todayDate.getDate();
                    value.today = todayDate;
                    value.inputDate = todayDate;
                }
            });
4

4 回答 4

3

只需使用三元运算符:

return $(this).each(function () {
       var todayDate = NoteWorklist.getDateTime(coffeeID == "showCoffeeId" ? "appleTime" : "orangeTime");
       value.Year = todayDate.getFullYear();
       value.Month = todayDate.getMonth() + 1;
       value.Day = todayDate.getDate();
       value.today = todayDate;
       value.inputDate = todayDate;
});
于 2013-09-19T15:06:29.087 回答
0

你当然可以:

return $(this).each(function () {

    var todayDate = NoteWorklist.getDateTime(coffeeId == "showCoffeeId" ? "appleTime" : "orangeTime");
    value.Year = todayDate.getFullYear();
    value.Month = todayDate.getMonth() + 1;
    value.Day = todayDate.getDate();
    value.today = todayDate;
    value.inputDate = todayDate;

});
于 2013-09-19T15:06:18.250 回答
0
    if (coffeeId == "showCoffeeId") {
       var todayDate = NoteWorklist.getDateTime("appleTime");
    } else {
      var todayDate = NoteWorklist.getDateTime("orangeTime");
    }

    value.Year = todayDate.getFullYear();
    value.Month = todayDate.getMonth() + 1;
    value.Day = todayDate.getDate();
    value.today = todayDate;
    value.inputDate = todayDate;
于 2013-09-19T15:06:29.290 回答
0

像这样?

return $(this).each(function () {
      var word =""
      if (coffeeId == "showCoffeeId") {
           word = "appleTime";
      } else {
           word = "orangeTime";
      }
     var todayDate = NoteWorklist.getDateTime(word);
     value.Year = todayDate.getFullYear();
     value.Month = todayDate.getMonth() + 1;
     value.Day = todayDate.getDate();
     value.today = todayDate;
     value.inputDate = todayDate;
});
于 2013-09-19T15:07:48.120 回答