本文实例讲述了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,showDate){
this.bookings[bookingId] = new CustomerBooking(bookingId,showDate);
}
//getBookingsTable方法
cinema.prototype.getBookingsTable = function(){
var booking;
var bookingsTableHTML="";
for(booking in this.bookings){
bookingsTableHTML +="