JavaScript構建自己的對象示例
更新時間:2016年11月29日 10:44:26 作者:牛逼的霍嘯林
這篇文章主要介紹了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程序設計有所幫助。
相關文章
connection reset by peer問題總結及解決方案
這篇文章主要介紹了connection reset by peer問題解決方案的相關資料,這里整理了一些常見問題,及如何解決,需要的朋友可以參考下2016-10-10
JavaScript下通過的XMLHttpRequest發(fā)送請求的代碼
JavaScript下通過的XMLHttpRequest發(fā)送請求的代碼,需要的朋友可以參考下。2011-06-06
解決Babylon.js中AudioContext was not allowed&nbs
這篇文章主要介紹了解決Babylon.js中AudioContext was not allowed to start異常問題方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04

