DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> JavaScript構建自己的對象示例
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程序設計有所幫助。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved