 |
BOOK: Beginning JavaScript 4th Edition
 | This is the forum to discuss the Wrox book Beginning JavaScript, 4th Edition by Paul Wilton, Jeremy McPeak; ISBN: 978-0-470-52593-7 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning JavaScript 4th Edition section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

May 9th, 2011, 05:02 PM
|
|
Registered User
|
|
Join Date: Apr 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Page 143 - this statement - Defining a Reference Type
I am kind of confused about the this in the beginning object function CustomerBookings. I do understand that there is the Object and an instance of an Object and all of that, and for the most part, understand most of the code... But the this in this line for example from below.
Code:
this.customerName = customerName;
Could someone break it down to me. I have read a couple of books, but it's not fully explained by glossed over. I am one that can't move on until I get this.
Thanks
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Chapter 5: Example 8</title>
</head>
<body>
<h2>Summary of bookings</h2>
<script type="text/javascript">
// CustomerBooking type
function CustomerBooking(bookingId, customerName, film, showDate)
{
this.customerName = customerName;
this.bookingId = bookingId;
this.showDate = showDate;
this.film = film;
}
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;
}
CustomerBooking.prototype.getBookingId = function()
{
return this.bookingId;
}
CustomerBooking.prototype.setBookingId = function(bookingId)
{
this.bookingId = bookingId;
}
// Cinema type
function Cinema()
{
this.bookings = new Array();
}
Cinema.prototype.addBooking = function(bookingId, customerName, film, showDate)
{
this.bookings[bookingId] = new CustomerBooking(bookingId,
customerName, film, showDate);
}
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>";
bookingsTableHTML += "</tr>";
}
bookingsTableHTML += "</table>";
return bookingsTableHTML;
}
var londonOdeon = new Cinema();
londonOdeon.addBooking(342, "Arnold Palmer","Toy Story", "15 July 2009 20:15");
londonOdeon.addBooking(335, "Louise Anderson",
"The Shawshank Redemption", "27 July 2009 11:25");
londonOdeon.addBooking(566, "Catherine Hughes",
"Never Say Never", "27 July 2009 17:55");
londonOdeon.addBooking(324, "Beci Smith","Shrek", "29 July 2009 20:15");
document.write(londonOdeon.getBookingsTable());
</script>
</body>
</html>
Last edited by londel26; May 9th, 2011 at 05:03 PM..
Reason: sentence placement.
|
|

May 9th, 2011, 11:31 PM
|
 |
Wrox Author
|
|
Join Date: Nov 2005
Posts: 87
Thanks: 0
Thanked 18 Times in 17 Posts
|
|
Howdy, londel26.
Whenever you see this in code, you can translate that into "this object/instance". When you call the CustomerBooking() function with the new operator, you create a new CustomerBooking object, and the constructor creates properties for that object. So this code:
Code:
this.customerName = customerName;
Can be translated into: "set this CustomerBooking object's (or instance's) customerName property to the value of the customerName parameter." It allows us to reference the current object inside constructor functions and methods. Does that help?
|
|

May 11th, 2011, 05:18 PM
|
|
Registered User
|
|
Join Date: Apr 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
This.that
Ok, so, "this" I guess then would not be a variable or a property, method or otherwise... it's just needed when using objects to state "this" object? And java just needs it because you are talking about an object? And otherwise it does not hold info or do anything other than call, or pull... this specific object. And, if I understand right... that if you had two objects... the "this" would only apply to the object we're in at the moment?
I guess what confused me was that we're just now creating the object, so where are we pulling the customerName var from if we just created it. But I guess that is the point? We're declaring them all at the same time?
Sorry for so many questions.. Just hard for me to move on if I am stuck.. LOL
James
|
|

July 18th, 2011, 09:01 AM
|
 |
Wrox Author
|
|
Join Date: Nov 2005
Posts: 87
Thanks: 0
Thanked 18 Times in 17 Posts
|
|
Howdy, londel26.
First, I apologize for this extremely late response. And to answer your first set of questions: yes. You will hear "this" sometimes be referred to "the this variable," but that's only because only saying "this" can get a little confusing.
We don't have to create properties all at once, but that's typically what a constructor function does; it constructs the object. So in the case of the CustomerBooking data type, we create the customerName property so that we can keep track of that data and use it whenever we need to.
|
|
 |