การคัดลอกรหัสมีดังนี้:
<! doctype html>
<html>
<head>
<meta charset = "utf-8">
<title> แทรกชื่อเรื่องที่นี่ </title>
<script type = "text/javascript">
-
* โหมดการรวมกัน: โหมดตัวสร้าง + โหมดต้นแบบ
วิธีนี้เป็นวิธีที่พบบ่อยที่สุดในการสร้างวัตถุใน JavaScript
แอตทริบิวต์ประเภทตัวแปร: ส่งผ่านด้วยตัวสร้าง
แอตทริบิวต์ประเภทฟังก์ชัน: ประกาศด้วยโหมดต้นแบบ
-
ฟังก์ชั่นนักเรียน (ชื่ออายุ) {
this.name = ชื่อ;
this.age = อายุ;
-
student.prototype.setName = function (name2) {
this.name = name2;
-
student.prototype.getName = function () {
ส่งคืนสิ่งนี้ชื่อ;
-
var stu1 = นักเรียนใหม่ ("Xiao Hu", 21);
การแจ้งเตือน (stu1.getName ());
</script>
</head>
<body>
</body>
</html>