JS學習筆記(三) 對象

來源:互聯網
上載者:User

標籤:

  參考資料:

  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。

  

  • 建立Javascript對象 
<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學習筆記(三) 對象

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.