標籤:
參考資料:
1. http://www.w3school.com.cn/js/js_objects.asp
? 知識點:
? Javascript中的所有事物都是對象。
? Javascript是基於prototype,而不是基於類的。
? Javascript是物件導向的語言,但Javascript不使用類。因此,在Javascript中,不會建立類,也不會通過類來建立對象。
? Javascript中的所有數字都儲存為根為10的64位的浮點數。其中,整數最多為15位,小數最大位元為17,且浮點運算並不總是100%準確的。
? Javascript中如果數位首碼為0,則該數值為八位元。如果首碼為0x,則該數值為十六進位數。
? 完整的Number數值對象參考手冊:http://www.w3school.com.cn/jsref/jsref_obj_number.asp。
<script>person=new Object();person.firstname="Bill";person.lastname="Gates";person.age=56;person.eyecolor="blue";
document.write(person.firstname + " is " + person.age + " years old.");</script>
<script>person={firstname:"Bill",lastname:"gates",age:56,eyecolor:"blue"}document.write(person.firstname + " is " + person.age + " years old.");</script>
<script>function person(firstname,lastname,age,eyecolor){this.firstname=firstname;this.lastname=lastname;this.age=age;this.eyecolor=eyecolor;}
// 使用物件建構器myFather=new person("Bill","Gates",56,"blue");document.write(myFather.firstname + " is " + myFather.age + " years old.");</script>
document.write(Math.round(4.7)) //四捨五入,輸出5document.write(Math.random()) //0和1之間的隨機數document.write(Math.floor(Math.random()*11)) //0和10之間的隨機整數
更多Math對象使用方法:http://www.w3school.com.cn/jsref/jsref_obj_math.asp。
1. 擷取當日的日期和時間。
document.write(Date())
結果:
2. 擷取從1970到1月1日至今的毫秒數。
document.write((new Date()).getTime());
結果:
3. 設定具體日期。
var d = new Date()d.setFullYear(2015,10,1)// 月份是從0到11document.write(d)
結果:
4. 將當前日期根據UTC轉換為字串。
document.write (new Date().toUTCString())
結果:
5. 顯示星期幾。
var d=new Date()var weekday=new Array(7)weekday[0]="星期日"weekday[1]="星期一"weekday[2]="星期二"weekday[3]="星期三"weekday[4]="星期四"weekday[5]="星期五"weekday[6]="星期六"document.write("今天是" + weekday[d.getDay()])
結果:
6. 比較日期
var myDate=new Date();myDate.setFullYear(2008,8,9);var today = new Date();if (myDate>today){ alert("Today is before 9th August 2008");}else{ alert("Today is after 9th August 2008");}
7. 顯示數字時鐘
<html><head><script type="text/javascript">function startTime(){ var today=new Date() var h=today.getHours() var m=today.getMinutes() var s=today.getSeconds() // add a zero in front of numbers<10 m=checkTime(m) s=checkTime(s) document.getElementById(‘txt‘).innerHTML=h+":"+m+":"+s t=setTimeout(‘startTime()‘,500)}function checkTime(i){ if (i<10) {
i="0" + i
}
return i}</script></head><body onload="startTime()"><div id="txt"></div></body></html>
結果:
JS學習筆記(三) 對象