2016-12-03 58 views
-1

我想在javaScript中更改日期格式,並且我發現這個代碼非常好,「Mike Christensen」。 但我的問題是關於什麼是在JavaScript陣列碼在VAR月線3含義使用兩種支架。在javascript數組中使用兩個括號是什麼意思

var mydate = new Date(form.startDate.value); 
var month = ["January", "February", "March", "April", "May", "June", 
"July", "August", "September", "October", "November", "December"] [mydate.getMonth()]; 
var str = month + ' ' + mydate.getFullYear(); 
+0

你與節約幾行初始化的VAR月直接... –

+0

考慮接受一個答案,如果它是有幫助 – Geeky

回答

2

他是用他得到它從mydate.getMonth月號()

在此代碼

這裏,mydate.getMonth()是11和在他本月索引數組索引月份陣列一個月[11]將在12月

var mydate = new Date(); 
 
console.log(mydate.getMonth()); 
 
var month = ["January", "February", "March", "April", "May", "June", 
 
"July", "August", "September", "October", "November", "December"] [mydate.getMonth()]; 
 
console.log(month); 
 
var str = month + ' ' + mydate.getFullYear();

希望你能理解

0

第一對括號的是創建陣列,可以考慮像這樣

var month = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] 

下一對括號的是用於通過索引陣列檢索操作。

someArr[index] 

你的情況

month[11] 

就是這樣,筆者只是指數,其中指數是從另一個變量中提取拿剛剛創建數組的元素。

0

這樣做:就是給init VAR一個月以前聲明數組中的元素只有一個非常廉價的方式...(東西會節省你寫一行多個但恕我直言降低代碼的可讀性... ) 所以斷裂下來:

var month = ["January", "February", "March", "April", "May", "June", 
"July", "August", "September", "October", "November", "December"] [mydate.getMonth()]; 

相同

var monthOptionsArray = ["January", "February", "March", "April", "May", "June", 
"July", "August", "September", "October", "November", "December"]; 

var monthIndex = mydate.getMonth(); 
var monthActual = monthOptionsArray[monthIndex]; 

實施例:

var mydate = new Date(); 
 
console.log(mydate.getMonth()); 
 
var month = ["January", "February", "March", "April", "May", "June", 
 
"July", "August", "September", "October", "November", "December"]; monthI = month[1]; 
 
console.log(monthI); 
 
var str = month + ' ' + mydate.getFullYear();

相關問題