Maison > interface Web > js tutoriel > JavaScript构建自己的对象示例

JavaScript构建自己的对象示例

高洛峰
Libérer: 2016-12-03 15:23:22
original
1309 Les gens l'ont consulté

本文实例讲述了JavaScript构建自己的对象。分享给大家供大家参考,具体如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

<script type=&#39;text/javascript&#39;>

//构建一个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>

Copier après la connexion


Étiquettes associées:
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Derniers numéros
c++ appelle javascript
Depuis 1970-01-01 08:00:00
0
0
0
Qu’est-ce que le garbage collection JavaScript ?
Depuis 1970-01-01 08:00:00
0
0
0
Que sont les fonctions de hook JavaScript ?
Depuis 1970-01-01 08:00:00
0
0
0
Comment obtenir la date actuelle en JavaScript ?
Depuis 1970-01-01 08:00:00
0
0
0
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal