JS常用內容

原生

獲取當前日期的字符型

function getCurrentDate(){
	var date= new Date();
	var year = 0;
	var month = 0;
	var day = 0;
	var currentDate = "";
	//初始化時間
	year = date.getFullYear();//ie火狐下都可以
	month = date.getMonth() + 1;//國外是0-11代表12個月;
	day = date.getDate();
	currentDate = year + "-";
	
	if(month >= 10){
		currentDate += month + "-";
	}else{
		currentDate += "0" + month + "-";
	}
	
	if(day >= 10){
		currentDate += day;
	}else{
		currentDate += "0" + day;
	}
	return currentDate;
}
console.log(getCurrentDate());//測試

遍歷數組

//初始化一個字符數組
var stringArray = new Array(5);
stringArray[0] = "I";
stringArray[1] = "am";
stringArray[2] = "doctorToliet";
stringArray[3] = "from";
stringArray[4] = "HeBei province";
var sentence = "";
//遍歷數組
for(var index in stringArray){
	sentence += stringArray[index] + " ";
}
console.log(sentence);

遍歷Map鍵值對

//初始化Map
var featureMap = {};
featureMap["name"] = "doctorToliet";
featureMap["home"] = "QianAn city HeBei province";
featureMap["age"] = 30;
featureMap["hobby"] = "e-sports,hip-hop";
var introduceMyself = "Hello guys!";
//遍歷Map鍵值對
for(var key in featureMap){
	introduceMyself += "my " + key + " is " + featureMap[key] + ";\n";
}
console.log(introduceMyself);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章