Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > BOOK: Beginning JavaScript 4th Edition
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 9th, 2011, 05:02 PM
Registered User
 
Join Date: Apr 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.
Reply With Quote
  #2 (permalink)  
Old May 9th, 2011, 11:31 PM
jmcpeak's Avatar
Wrox Author
Points: 467, Level: 7
Points: 467, Level: 7 Points: 467, Level: 7 Points: 467, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2005
Location: , Texas, USA.
Posts: 87
Thanks: 0
Thanked 18 Times in 17 Posts
Default

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?
Reply With Quote
  #3 (permalink)  
Old May 11th, 2011, 05:18 PM
Registered User
 
Join Date: Apr 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
Reply With Quote
  #4 (permalink)  
Old July 18th, 2011, 09:01 AM
jmcpeak's Avatar
Wrox Author
Points: 467, Level: 7
Points: 467, Level: 7 Points: 467, Level: 7 Points: 467, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2005
Location: , Texas, USA.
Posts: 87
Thanks: 0
Thanked 18 Times in 17 Posts
Default

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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Go from 2d Array to 1d array without defining type OneQuestion General .NET 1 January 10th, 2008 11:13 AM
problem defining namespace for .aspx page pratik28 ASP.NET 2.0 Basics 0 October 26th, 2006 06:43 AM
Defining Classes for Elements; page 734 Nick Y BOOK: Ivor Horton's Beginning Visual C++ 2005 0 July 23rd, 2006 04:10 AM
defining location for input type file bleitner Javascript How-To 1 August 24th, 2004 08:15 AM



All times are GMT -4. The time now is 10:37 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.