我在js函数中有一个js代码...
它包含其他条件...包含相同功能的只是参数更改...
所以唯一不同的是您要传递给函数的参数字符串...
使用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;
}
});
最佳答案
只需使用三元运算符:
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;
});