JavaScript使用构造函数与原型构建自定义对象详解
作者:青柠时间:2026-10-30 17:37:02浏览:414
本文通过具体示例展示如何在JavaScript中构建自定义对象。重点讲解利用构造函数初始化实例属性,并通过原型对象定义共享方法,如获取和设置预订ID。文章包含完整的代码实现,帮助开发者理解JavaScript面向对象编程的核心机制。
JavaScript自定义对象构建示例
本文通过具体实例介绍如何在JavaScript中创建自定义对象,供读者参考学习。
构造函数与原型方法实现
<script type='text/javascript'>
//构建一个CustomerBooking类
//构造函数
function CustomerBooking(bookingId,customerName,film,showDate){
this.bookingId = bookingId;
this.customerName = customerName;
this.film = film;
this.showDate =showDate;
}
//getBookingId方法,有点奇特
CustomerBooking.prototype.getBookingId = function(){
return this.bookingId;
}
//setBookingId方法
CustomerBooking.prototype.setBookingId = function(bookingId){
this.bookingId = bookingId;
}
CustomerBooking.prototype.getCustomerName = function(){
return this.customerName;
}
CustomerBooking.prototype.setCustomerName = function(customerName){
this.customerName = customerName;
}
CustomerBooking.prototype.getFilm = function(){
return this.film;
}
CustomerBooking.prototype.setFilm = function(film){
this.film = film;
}
CustomerBooking.prototype.getShowDate = function(){
return this.showDate;
}
CustomerBooking.prototype.setShowDate = function(showDate){
this.showDate = showDate;
}
//构建一个cineme类,属性为数组,可以保存预定信息
function cinema(){
this.bookings = new Array();
}
//addBooking方法
cinema.prototype.addBooking = function(bookingId,customerName,film,showDate){
this.bookings[bookingId] = new CustomerBooking(bookingId,customerName,film,showDate);
}
//getBookingsTable方法
cinema.prototype.getBookingsTable = function(){
var booking;
var bookingsTableHTML="<table border=1>";
for(booking in this.bookings){
bookingsTableHTML +="<tr><td>";
bookingsTableHTML +=this.bookings[booking].getBookingId();
bookingsTableHTML +="</td>";
bookingsTableHTML +="<td>";
bookingsTableHTML +=this.bookings[booking].getCustomerName();
bookingsTableHTML +="</td>";
bookingsTableHTML +="<td>";
bookingsTableHTML +=this.bookings[booking].getFilm();
bookingsTableHTML +="</td>";
bookingsTableHTML +="<td>";
bookingsTableHTML +=this.bookings[booking].getShowDate();
bookingsTableHTML +="</td></tr>";
}
bookingsTableHTML +="</table>";
return bookingsTableHTML;
}
//新建cinema对象就可以了,这里会通过addBooking自动生成customerBooking对象,
保存到cinema对象bookingFilm的属性当中,然后调用getBookingsTable方法来获取数据信息
var bookingFilm = new cinema();
bookingFilm.addBooking(123,"Jack","Love Java","1 May 2012");
bookingFilm.addBooking(123,"Jack","Love Java","1 May 2012");
bookingFilm.addBooking(122,"Jack","Love Java","1 May 2012");
bookingFilm.addBooking(121,"Jack","Love Java","1 May 2012");
bookingFilm.addBooking(120,"Jack","Love Java","1 May 2012");
bookingFilm.addBooking(119,"Jack","Love Java","1 May 2012");
document.write(bookingFilm.getBookingsTable());
</script>
如需深入了解JavaScript相关技术,可参考本站系列教程,包括《javascript面向对象入门教程》、《JavaScript中json操作技巧总结》、《JavaScript切换特效与技巧总结》、《JavaScript查找算法技巧总结》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》。
希望本文内容能对您的JavaScript编程实践提供有益帮助。
作者最新文章
ES6模块化中js文件怎么用import和export交互
2026-11-05 17:00:48
JS读取本地文件时报错或无响应的常见原因
2026-11-05 16:32:31
let、const与var的区别:JavaScript变量声明最佳实践
2026-11-05 14:16:38
JS中直接定义静态二维数组的字面量写法
2026-11-05 14:16:08
JS 怎么查找字符串中所有匹配项的位置
2026-11-05 14:15:53
