本文实例讲述了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程序设计有所帮助。
标签:
JavaScript,构建对象
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“JavaScript构建自己的对象示例”评论...
更新动态
2024年11月15日
2024年11月15日
- 群星-时尚慢摇DJ舞曲《发烧车载中文天碟-调情》非卖品[WAV]
- 潘美辰.2008-全新重声大碟(出道20年精选纪念版)【倍特音乐】【WAV+CUE】
- 罗时丰.2002-唱歌的人(2011再生版)【贝特音乐】【WAV+CUE】
- 罗时丰.2003-唱歌的人台语精选+新歌【贝特音乐】【WAV+CUE】
- 999PUNKSTA《情绪数码故障》[Hi-Res][24bit 48kHz][FLAC/分轨][301.83MB]
- HOYO-MiX《原神-珍珠之歌4 游戏音乐》[320K/MP3][289.48MB]
- 陈崎凡《CHEN》[320K/MP3][81.13MB]
- skt都在哪一年夺冠 英雄联盟skt夺冠赛季介绍
- 炉石传说抢先体验乱斗什么时候结束 深暗领域体验乱斗结束时间
- 炉石传说抢先乱斗卡组有什么 深暗领域抢先体验乱斗卡组推荐
- 荣耀手机腕上最佳搭档 荣耀手表5首销开启
- 雷克沙ARES 6000 C28战神之翼 AMD 9800X3D超强搭档
- 咪咕快游感恩同游,超值回馈尽在咪咕咪粉节!
- 陈崎凡《CHEN》[FLAC/分轨][326.32MB]
- 群星《我们的歌第六季 第2期》[320K/MP3][74.05MB]