SoFunction
Updated on 2025-03-01

JavaScript builds your own object example

This article describes JavaScript building its own objects. Share it for your reference, as follows:

<script type='text/javascript'>
//Build a CustomerBooking class//Constructorfunction CustomerBooking(bookingId,customerName,film,showDate){
   = bookingId;
   = customerName;
   = film;
   =showDate;
}
//getBookingId method, a bit strange = function(){
  return ;
}
//setBookingId method = function(bookingId){
   = bookingId;
}
 = function(){
  return ;
}
 = function(customerName){
   = customerName;
}
 = function(){
  return ;
}
 = function(film){
   = film;
}
 = function(){
  return ;
}
 = function(showDate){
   = showDate;
}
//Construct a cineme class with an attribute as an array, and you can save predefined informationfunction cinema(){
   = new Array();
}
//addBooking method = function(bookingId,customerName,film,showDate){
  [bookingId] = new CustomerBooking(bookingId,customerName,film,showDate);
}
//getBookingsTable method = function(){
  var booking;
  var bookingsTableHTML="<table border=1>";
  for(booking in ){
    bookingsTableHTML +="<tr><td>";
    bookingsTableHTML +=[booking].getBookingId();
    bookingsTableHTML +="</td>";
    bookingsTableHTML +="<td>";
    bookingsTableHTML +=[booking].getCustomerName();
    bookingsTableHTML +="</td>";
    bookingsTableHTML +="<td>";
    bookingsTableHTML +=[booking].getFilm();
    bookingsTableHTML +="</td>";
    bookingsTableHTML +="<td>";
    bookingsTableHTML +=[booking].getShowDate();
    bookingsTableHTML +="</td></tr>";
  }
  bookingsTableHTML +="</table>";
  return bookingsTableHTML;
}
// Just create a new cinema object. Here, the customerBooking object will be automatically generated through addBooking.Save tocinemaObjectbookingFilmAmong the properties of,Then callgetBookingsTableMethod to obtain data information
var bookingFilm = new cinema();
(123,"Jack","Love Java","1 May 2012");
(123,"Jack","Love Java","1 May 2012");
(122,"Jack","Love Java","1 May 2012");
(121,"Jack","Love Java","1 May 2012");
(120,"Jack","Love Java","1 May 2012");
(119,"Jack","Love Java","1 May 2012");
(());
</script>

For more information about JavaScript, please view the special topic of this site: "JavaScript object-oriented tutorial》、《Summary of json operation skills in JavaScript》、《Summary of JavaScript switching effects and techniques》、《Summary of JavaScript search algorithm skills》、《Summary of JavaScript Errors and Debugging Skills》、《Summary of JavaScript data structure and algorithm techniques》、《JavaScript traversal algorithm and skills summary"and"Summary of JavaScript mathematical operations usage

I hope this article will be helpful to everyone's JavaScript programming.