語法保存? 保存一個var;把它放在了頭
// date.length === format.length
for (var m, d, y, i = 0, len = format.length; i < len; i++) {
if (/m/.test(format[i])) m = date[i]
if (/d/.test(format[i])) d = date[i]
if (/y/.test(format[i])) y = date[i]
}
或者,也許條件語句:
for (var m, d, y, i = 0, len = format.length; i < len; i++) {
(/m/.test(format[i])) ? m = date[i]
:(/d/.test(format[i])) ? d = date[i]
:(/y/.test(format[i])) y = date[i]
: continue;
}
但是,這改變了邏輯和IM不知道這是想
也許你可以添加一個繼續執行速度更快,但再次不確定邏輯
for (var m, d, y, i = 0, len = format.length; i < len; i++) {
if (/m/.test(format[i])){
m = date[i]
//jump to next since this has been found
continue;
}
if (/d/.test(format[i])){
d = date[i]
continue;
}
if (/y/.test(format[i])){
y = date[i]
continue;
}
}
如果你試圖解釋你想達到什麼,那麼給你一個解決方案會更容易。 – jishi
這段代碼真的可以做任何事情,只要'format [i]'的位置中的字符串匹配''m'',''d就將'm','d'或'y'賦值給'date [i] 「'或'」y「'分別。 – elclanrs