# javascript object 物件 ###### tags: `javascript` `object` ### Login input information (creating object) ```javascript= var jsbook = {title:'Javascript starter book',price: 500, stock: 3} console.log(jsbook) ``` ### Reading the information inside the object ```javascript= var jsbook = {title:'Javascript starter book',price: 500, stock: 3} console.log(jsbook.price) console.log(jsbook['price']) jsbook.stock= 10; console.log(jsbook.stock) ``` ### read all type(class) (for.. in ..) ```javascript= var jsbook = {title:'Javascript starter book',price: 500, stock: 3} for(var p in jsbook) { console.log(p + '='+ jsbook[p]); } ``` ### input to html ```htmlmixed= <!DOCTYPE html> <!-- 物件(object) 檔名請設為: ObjectTest.html --> <html> <head> <section> <!-- 將所有項目輸出至HTML --> <table> <tr> <td id="title"></td> <td id="price"></td> <td id="stock"></td> </tr> </table> </section> </head> <body> <script> var jsbook = {title:'javascript 入門書', price: 500, stock: 3} document.getElementById('title').textContent = jsbook.title; document.getElementById('price').textContent = jsbook.price+'元'; document.getElementById('stock').textContent = jsbook.stock; </script> </body> </html> ``` ## Show a readable date formatre - [Date 物件] ```htmlmixed= var now = new Date(); var year = now.getFullYear(); var month = now.getMonth(); var date = now.getDate(); var hour = now.getHours(); var min = now.getMinutes(); var sec = now.getSeconds(); var ampm = ' '; if(hour < 12){ ampm = 'a.m.' }else{ ampm = 'p.m.' } var output = year+"/"+(month+1)+"/"+date+"_"+(hour%12)+":"+min+":"+ampm; document.getElementById('time').textContent = output ``` ### There is still some object that don't need to do a startup setting 1. Math.random(); 2. windows.alert(); 3. document.getElementById(); How to know? 1. Can make alot of obeject at the start <!DOCTYPE html> <html> <head> </head> <body> <script> var addZero = function(num, digit){ var numString = String(num) ; while(numString.length < digit){ numString = '0' + numString } return numString; } //console.log(addZero(1,2)) //Please put the 1 two the tenth decimal var songs = [ 'Starlight', 'Doll', 'Car', 'Be a song', 'My Favorite Things', 'Taking A Chance On Love', 'Fly Me To The Moon', 'Waltz For Debby', 'Willow Weep For Me', 'Bluesette' ]; for(var i - 0; i < song.length; i++){ var paragraph = documentElement('p') paragraph.textContent = addZero(i + 1,2) +'. '+song[i]; } </script> <dy> <ml> </body> </html>