สอนทำเว็บขั้นพื้นฐานJavaScript
การออกคำสั่ง,เรียกใช้
ภาษา Java Script นั้นเป็นภาษาที่เขียนในลักษณะเชิงวัตถุ(Object) และแต่ละ object ก็มีส่วนประกอบและความสามารถที่แตกต่างกัน object ที่ใช้บ่อยที่สุดคือ document, window
วิธีการเรียกชื่อก็ให้เรียกรูปแบบหรือชนิดของวัตถุชนิดนั้นก่อน ต่อไป ก็เรียกชื่อย่อยที่เราตั้งขึ้นโดยใช้จุดเป็นตัวคั่น
Object แต่ละชนิดจะมีคุณสมบัติเฉพาะตัวของมันเองและสามารถที่จะกำหนด ควบคุมคุณสมบัติเหล่านั้นได้ โดยใช้จุดเป็นตัวคั่นเช่นกัน
| Object.Name.Property |
การเรียกใช้ก็คล้ายการเรียกชื่อเพียงแต่ระบุชื่อของ Object ตามด้วยชื่อของวิธีการทำงานหรือที่เรียกว่า Method แล้วก็ตามด้วยค่าที่วิธีการทำงานนั้นต้องใช้ ( Argument )
| Object.Name.Method(Argument) |
สำหรับ Window และ Name อาจละไว้ได้ในบางโอกาส เช่น
document.write("write this text to the current window") มันก็จะเขียนข้อความนี้ลงไปใน webpage และจะได้ผลเหมือนกับ window.document.write("write this text to the current window") เนื่องจาก document เป็น properties ของ window
ตัวอย่าง
<script>
var example
example=document.lastModified
document.write("This page was last modified: "+example)
</script>
example คือตัวแปรที่ถูกกำหนดให้เป็น document.lastModified
lastModified เป็น property ของ document หมายถึงวันที่ Document ถูก Update ครั้งสุดท้าย
write คือ method ของ object document
argument คือ ("This page was last modified:"+example)
ผลลัพธ์ที่แสดงออกมาคือ
This page was last modified: 10/30/97 03:40:56
เมื่อใดก็ตามที่คุณ update และ save page ของคุณ script นี้จะเปลี่ยนวันเวลาให้ทันที
นำ script นี้ไปใส่ใน page ของคุณได้เลย
อีกสักตัวอย่างนะครับ
<script>
document.writeln("Please thank this site for adding a link to me!")
document.write(document.referrer)
</script>
writeln คือ method ของ document หมายถึงเขียนโดยขึ้นบรรทัดใหม่
referer คือ property ของ document หมายถึง URL ของ document ที่ links มาที่ document นี้
| Properties of Document Object |
| Properties of Document Object | |
| Property | Description |
| alinkColor | ให้ค่าสีของ alink |
| anchor | An object that refers to an array contained in a document |
| anchors | An array of all the anchors contained in a document |
| applet | เป็น Object ที่หมายถึง applet ใน document |
| applets | เป็น Array ของ Applet ใน document |
| area | เป็น Object หมายถึง Image map Area ใน document |
| bgColor | ให้ค่าของสีพื้นหลัง |
| cookie | ให้ค่า cookies |
| domain | ให้ค่า domain name ของ document server |
| embeds | array ของ plug-in ใน document |
| fgColor | ให้ค่าสีตัวอักษรปกติ (text) |
| form | Object ของ form ใน Document |
| forms | เป็น Array ของ form ใน Document |
| history | เป็น List ของ URL ที่คุณเคยไป ใน window ปัจจุบัน |
| image | Image Object ใน Document |
| images | Array ของ Image |
| lastModified | วันที่ Document ถูก Update ครั้งสุดท้าย |
| link | ตัว link ใน document |
| links | Array ของ links |
| linkColor | สีของ link |
| plugin | plugin ใน document |
| referer | URL ของ document ที่ links มาที่ document นี้ |
| title | title ของ document |
| URL | URL ของ Document |
| vlinkColor | สีของ vlinks |
property เยอะๆอย่างนี้ไม่ต้องสนใจมาก เอาเป็นว่าคุณได้ ideaก่อน ก็พอแล้ว
| Function , สร้าง Function ของคุณเอง |
Function คือ code java script พร้อมที่จะทำงานเมื่อมีการเรียกใช้
พื้นฐาน syntax ของ function คือ:
function whatever_name()
{
function codes are entered here
}
ตัวอย่าง:
function test()
{
document.write("Hello there!")
}
function ของเราชื่อ test()
code ของ function คือ document.write("Hello there!")
ถ้าจะให้มีการเรียกใช้ต้องเขียนแบบนี้
<script>
function test()
{
document.write("Hello there!")
}
test()
</script>
ถ้าไม่มีบรรทัด test() หมายถีงไม่มีการเรียกใช้ function เมื่อเปิดเอกสารมาก็จะว่างเปล่า เพราะมีแต่ function ไม่มีการเรียกใช้
ตัวอย่าง พื้นที่สามเหลี่ยม=1/2 คูณ ฐาน คูณ สูง
function area(b,h)
{
var area=1/2*b*h
alert(area+" sq ft")
}
area(2,2)
area(3,5)
area(4,8)
<script>
var x=prompt("กรุณาใส่อายุของคุณ")
function calsecs(age)
{
var temp=age*365*24*60*60
alert("คุณมีอายุ "+temp+" วินาทีแล้ว!")
}
calsecs(x)
</script>
prompt() เป็น method ของ window ใช้แสดงข้อความแล้วก็ ช่องใส่ข้อมูลให้ผู้ใช้กรอกข้อความ
จะพูดถีง method ของ window ในบทอื่นอดใจอ่านต่อนะครับ
| Functions that return a value |
function สามารถส่งค่ากลับให้ตัวมันเองได้ ดังตัวอย่าง
function diameter(x)
{
temp=2*x
>return temp
}
x คือ รัศมีวงกลม function นี้สามารถส่งค่า diameter ให้ไปใช้งานใน script ได้
<script>
var d=diameter(5) //d will contain 10
var a=3.14*diameter(5) //a will contain 31.4
</script>
function จะเป็นตัวดำเนินการหาค่า diameter จากรัศมี แล้วส่งค่าเป็นตัวแปรให้กับ script อ่านต่อๆไปแล้วจะรู้ว่า return value มีประโยชน์อย่างไร
function สามารถส่งค่ากลับได้เพียงค่าเดียว เหมือนกับตัวแปรที่มีค่าเดียว
ต่อไปเป็นตัวอย่างที่ผิด เพราะว่า
***** A function can only return one value *****
function illegal(x)
{
temp=2*x
temp2=2*2*x
return temp
return temp2
}
| สรุปบทนี้ |
-
Object.Name.Property
-
Object.Name.Method(Argument) - <script>
function whatever_name()
{
function codes are entered here
}
whatever_name()
</script>
- A function itself can return a value.
A function can only return one value, just like a variable can only contain one value at a time.















