Java Reference
In-Depth Information
Typically, a reference type is defi ned with an uppercase letter. Doing so makes it easy to differentiate a
function from a reference type easily and quickly.
When you look at the code, the important thing to note is that the constructor function's name must
match that of the type you are defi ning — in this case
CustomerBooking
. That way, when a new
instance of your type as an object (termed an
object instance
) is created, this function will be called auto-
matically. Note this constructor function has four parameters, and that these are used inside the defi ni-
tion itself. However, note that you use the
this
keyword. For example:
this.customerName = customerName;
Inside a constructor function or within a method, the
this
keyword refers to that object instance of
your reference type. This code refers to the
customerName
property of this instance object, and you
set it to equal the
customerName
parameter. If you have used other object-oriented programming lan-
guages, you might wonder where you defi ned this
customerName
property. The answer is that you
didn't; simply by assigning a property a value, JavaScript creates it for you. There is no check that the
property exists; JavaScript creates it as it needs to. The same is true if you use the object with a property
never mentioned in your type defi nition. All this free property creation might sound great, but it has
drawbacks, the main one being that JavaScript won't tell you if you accidentally misspell a property
name; it'll just create a new property with the misspelled name, something that can make it diffi cult to
track bugs. One way around this problem is to create methods that get a property's value and enable
you to set a property's value. Now this may sound like hard work, but it can reduce bugs or at least
make them easier to spot. Let's create a few property
get
/
set
methods for the
CustomerBooking
ref-
erence type.
CustomerBooking.prototype.getCustomerName = function()
{
return this.customerName;
}
CustomerBooking.prototype.setCustomerName = function(customerName)
{
this.customerName = customerName;
}
CustomerBooking.prototype.getShowDate = function()
{
return this.showDate;
}
CustomerBooking.prototype.setShowDate = function(showDate)
{
this.showDate = showDate;
}
CustomerBooking.prototype.getFilm = function()
{
return this.film;
}
CustomerBooking.prototype.setFilm = function(film)
{
this.film = film;