欢迎来到代码驿站!

JavaScript代码

当前位置:首页 > 网页前端 > JavaScript代码

JavaScript构建自己的对象示例

时间:2021-08-18 08:16:17|栏目: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程序设计有所帮助。

上一篇:create-react-app安装出错问题解决方法

栏    目:JavaScript代码

下一篇:用Javscript实现表单复选框的全选功能

本文标题:JavaScript构建自己的对象示例

本文地址:http://www.codeinn.net/misctech/167852.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有