เราสามารถเข้าถึงวัตถุที่มีอยู่ t โดยการสร้างต้นแบบของตัวเองโดยใช้วิธีจาวาสคริปต์ที่เรียกว่า "Object.create() " โดยใช้วิธีนี้เราสามารถสืบทอดคุณสมบัติจากคุณสมบัติที่มีอยู่ไปยังต้นแบบที่สร้างขึ้นใหม่ มาพูดคุยกันโดยสังเขป
ไวยากรณ์
Object.create(existing obj);
วิธีนี้ใช้วัตถุที่มีอยู่และสร้างต้นแบบของตัวเองเพื่อให้คุณสมบัติ สืบทอด จาก วัตถุที่มีอยู่ ไปยังต้นแบบที่สร้างขึ้นใหม่ .
ตัวอย่าง
ในตัวอย่างต่อไปนี้ เริ่มแรก วัตถุชื่อ "บุคคล " ถูกสร้างขึ้นและใช้ "Object.create " ต้นแบบของตัวเองถูกสร้างขึ้นและกำหนดให้กับตัวแปร "ใหม่ " ต่อมา เมื่อใช้ต้นแบบ ออบเจ็กต์ของอ็อบเจ็กต์ที่มีอยู่จะมีการเปลี่ยนแปลงและคุณสมบัติใหม่แสดงตามที่แสดงในผลลัพธ์
<html>
<body>
<script>
var person = {
name: "Karthee",
profession : "Actor",
industry: "Tamil"
};
document.write(person.name);
document.write("</br>");
document.write(person.profession);
document.write("</br>");
document.write(person.industry);
document.write("</br>");
document.write("Using a prototype the properties of the existing object have been
changed to the following");
document.write("</br>");
var newper = Object.create(person); /// creating prototype
newper.name = "sachin";
newper.profession = "crickter";
newper.industry = "sports";
document.write(newper.name);
document.write("</br>");
document.write(newper.profession);
document.write("</br>");
document.write(newper.industry);
</script>
</body>
</html> ผลลัพธ์
Karthee Actor Tamil Using a prototype the properties of the existing object have been changed to the following sachin crickter sports