當然啦!!要學習JavaScript通常必須和HTML結合,所以必須先有一段HTML的源代碼來當作示範:
<html> <head><title>Java Script</title></head> <script language="JavaScript"> function Init() { // 這一段是範例.... } </script> <body OnLoad="Init(); "> <center>Hello!! JavaScript!! </center> </body> </html>首先,在JavaScript裡也是可以有物件的,關鍵字是Object。有兩種表示方式:
function Init() { var objPerson = new Object(); objPerson.FirstName = "Pablo"; objPerson.LastName = "Picasso"; objPerson.Tel = "(02) 2887-9576"; objPerson.Mobile = "0800-024-365"; objPerson.Age = 32; alert( "objPerson.FirstName = " + objPerson.FirstName + "\n" ); alert( "objPerson.LastName = " + objPerson.LastName + "\n" ); alert( "objPerson.Tel = " + objPerson.Tel + "\n" ); alert( "objPerson.Mobile = " + objPerson.Mobile + "\n" ); alert( "objPerson.Age = " + objPerson.Age + "\n" ); return; }第二種方式:
function Init() { var objBody = { Height: 60, Weighy: 70, Long: 80, size: function () { return this.Height * this.Weight * this.Long; } } alert( "objBody.size(): " + objBody.size() + "\n" ); return; }從以上兩個寫法可以看出,JavaScript裡的所謂物件 (Object)觀念,非常類似C語言裡面的結構 (Struct)-都是複合式的資料型態。而從第二個例子當中,看到this這個關鍵字和它的用法,就比較類似C++裡的類別 (Class,this同樣都是指到目前所在物件的實體)。
除此之外,還有介紹JavaScript程式碼的撰寫風格:http://docs.jquery.com/JQuery_Core_Style_Guidelines
沒有留言:
張貼留言