Object.prototype คืออะไร วิธีใช้งานอย่างไร
prototype constructor จะช่วยให้เราสามารถเพิ่ม properties และ method ใหม่ ๆ ให้กับ object ได้ ในตัวอย่างนี้ จะใช้ Boolean() object
- เมื่อเรากำหนด property ใหม่ , object Boolean ทั้งหมด จะถูกสร้าง property และกำหนด value หรือค่า default ตามที่เรากำหนด
- เมื่อเรากำหนด method ใหม่ , object Boolean ทั้งหมด จะมี method นี้ทั้งหมด
จำไว้ว่า : Boolean.prototype ไม่สามารถใช้อ้างกับ boolean ที่ไม่ใช่แบบธรรมดาได้ สามารถใช้กับ Boolean แบบ object ได้อย่างเดียว
จำไว้ว่า : Prototype เป็น constructor ของ object ระดับ global ซึ่งจะส่งผลให้กับ JavaScript objects ทั้งหมดตัวอย่าง
Click the button, and display "Shield" if the Boolean is true, otherwise display "sword".
ตัวอย่างโค้ด
<html> <head> </head> <body>Click the button, and display "Shield" if the Boolean is true, otherwise display "sword".
</body> </html>