Get Programming with JavaScript - Listing 9.08
Listing 9.08 - A calendar event constructor
JS Bin
var CalendarEvent = function (title, startDate, startTime, endTime) {
this.title = title;
this.startDate = startDate;
this.startTime = startTime;
this.endTime = endTime;
this.showEvent = function () {
var dateString = [
this.startDate,
", from ",
this.startTime,
" to ",
this.endTime
].join("");
console.log(this.title);
console.log(dateString);
};
};
var calEvent = new CalendarEvent(
"Annual Review",
"3/5/16",
"4.00pm",
"5.00pm"
);
calEvent.showEvent();
Further Adventures
Listing 9.08 - A calendar event constructor - Tasks 1&2
- Add a second event.
- Call the showEvent method on your new calendar event.
var CalendarEvent = function (title, startDate, startTime, endTime) {
this.title = title;
this.startDate = startDate;
this.startTime = startTime;
this.endTime = endTime;
this.showEvent = function () {
var dateString = [
this.startDate,
", from ",
this.startTime,
" to ",
this.endTime
].join("");
console.log(this.title);
console.log(dateString);
};
};
var calEvent = new CalendarEvent(
"Annual Review",
"3/5/16",
"4.00pm",
"5.00pm"
);
// second event
var calEvent2 = new CalendarEvent(
"Staff Social",
"5/3/16",
"8.00pm",
"11.00pm"
);
calEvent.showEvent();
calEvent2.showEvent();
Listing 9.08 - A calendar event constructor - Task 3
- Update the showEvent method so that its output looks like this:
Annual Review: 3/5/16 - (4.00pm - 5.00pm)
var CalendarEvent = function (title, startDate, startTime, endTime) {
this.title = title;
this.startDate = startDate;
this.startTime = startTime;
this.endTime = endTime;
this.showEvent = function () {
var info = this.title + ": ";
info += this.startDate + " - ";
info += "(" + this.startTime + " - " + this.endTime + ")";
console.log(info);
};
};
var calEvent = new CalendarEvent(
"Annual Review",
"3/5/16",
"4.00pm",
"5.00pm"
);
var calEvent2 = new CalendarEvent(
"Staff Social",
"5/3/16",
"8.00pm",
"11.00pm"
);
calEvent.showEvent();
calEvent2.showEvent();