ITSE 1411 Beginning Web
JS Object-Oriented JavaScript Chapter 6 Discussion
Errata
  1. Typos in text
    • Pages 326 and 327,
      1. 6th line up from the bottom of page 326 there are 2 closing td tags. Should only be 1 closing td tag.
      2. On page 327, after the closing bracket of the "for" loop, the last line of the page is missing. Should be (please notify me if I am not correct)
        calendarWin.document.write("</tr>");
      3. On the practice for the number class in the function to calculate group rate, the math is not correct in the if clauses. It should be 49 *(.9 and .8 and .75) respectively instead of divided by.
    • Page 328, step 7 does not work for February during leap year. If you substitute

      var numDays = 0;
      numDays = daysInMonth(month, dateObject.getFullYear());

      for step 7, then add the following function to your JavaScript, you will correct that problem. (The comments are only for explanation purposes.)
                        // ********************************************************
                        // the code specified for getting days of month on pg 328 
                        // is flawed. It does not take into account whether the year
                        // is leap year which changes the number of days in Feb
                        // daysInMonth function takes care of days in leap and non-leap years
                        // this snippet came from http://snippets.dzone.com/posts/show/2099
                        function daysInMonth(iMonth, iYear)
                        {
                         return 32 - new Date(iYear, iMonth, 32).getDate();
                        }
                        // now that's an amazing function!
  2. FYI: if toLocaleString() results in comma-delimited American-style numbers. However, IE displays it with decimals. ex) $1,357.00 Firefox displays no decimals. ex) $1,357. (Figure 6-10 shows the value with no decimal point.)

  3. Vista Problems
    • Students have reported to me that they kept having errors. I couldn't see the errors. I found this has to do with Windows Vista and the "Computer" zone security vs. Internet security and "protected mode". One web site says to turn protected mode on for your local zone, while another site cautions about what a bad idea that is. When uploaded to the linux, it works fine from there.

      Therefore, if you keep getting unexplained errors and you are using Vista, please upload to the class server and see if the error still exists.
Discussion
  1. JavaScript Objects
    • Object-oriented programming (called OOP) allows you to reuse code without having to copy or recreate it.
    • OOP refers to the creation of reusable software objects that can be easily incorporated into multiple programs.
    • Objects (called components) specifically refer to the programming code and data that can be treated as an individual unit or component.
    • An object on a page can be as simple as a button.
  2. Encapsulation
    • Encapsulation is the process of bundling data with the methods (or other functions) operating on that data.
    • Encapsulation is sometimes called a black box because it allows for information hiding by restricting access to methods and properties.
  3. Garbage Collection
    • Garbage collection refers to reclaiming memory that is reserved by a program. Creating variables and objects reserves computer memory. JavaScript does not require that the program release memory. This is done automatically.
  4. Using the Date Class
    • To use the Date class you need to define objects of that type. Defining the object is similar to defining an array.
    • Used to work with dates and times.
    • Automatically holds the current date and time as its initial value.
    • Example of instantiating the Date Object (which will default to the current date):
      • var currentDate=new Date();
    • Example of overriding the default value with June 15, 2009 (see pages 320 - 321 for commonly used methods):
      • var anotherDate=new Date();
        anotherDate.setDate(15);
        anotherDate.setMonth(5); // January = 0
        anotherDate.setFullYear(2009);
    • Example of comparing currentDate to anotherDate:
      • if (currentDate < anotherDate)
           window.alert("The current date is before June 15, 2009");
        else
           window.alert("The current date is either June 15, 2009 or after June 15, 2009");
    • Example of changing the date to yesterday:
      • anotherDate.setDate(anotherDate.getDate()-1);
      • If subtracting one day from the date causes the date to be in the preceding month and/or year, the changes to the month and year portions of the date are handled automatically by the date class itself.
  5. The Number Class
    • Used to define, manipulate, and modify numeric values
    • Defines methods such as toFixed(), toPrecision(), toString(), valueOf(), toExponential(), Number(), toLocaleString()
    • Defines special values such as MAX_VALUE, MIN_VALUE, NaN, NEGATIVE_INFINITY, POSITIVE_INFINITY
  6. The Math Class
    • Used to perform mathematical calculations in your program.
    • The Math class does not contain a constructor. You use the Math object and one of its methods directly in your code. For example:

      squareRoot = Math.sqrt(144);

      would assign 12 to the variable squareRoot.
  7. Custom JavaScript Objects
    • JavaScript allows custom objects, but they are not encapsulated.
    • You may use standard functions and variables or create custom objects to perform the same functionality. Using custom objects to create self-contained objects makes programming easier.
    • The following is an example to create a custom object:

      var objectName = {};

    • You can assign properties to the object by appending the property name to the object name with a period.
    • To create custom objects that contain methods, you must use constructor functions.