Unit 9


Sample 9-1 shows using a function as a constructor to build an object.


sample 9-2 shows adding a method.

Extend an Object

sample 9-3 extend shows extending an Object by adding a property and a method.

Extend a class

sample 9-4 class shows extending a class. There are no real classes in JavaScript. A real class would force all the objects in the class to have the same properties. In JavaScript, the term class is commonly used to refer to all of the objects created with the same constructor function. Adding a property or method to the prototype property of the constructor function will add that property or method to every object in the class.

Object Constructor

sample 9-5 the Object constructor function is provided by JavaScript. Note that the function does not have a name, however it is a method, and the property that refers to the method does have a name.

Object literal

Sample 9-6 object literal shows use of a literal object. It also shows object notation and associative array notation.

Lab 3

In lab 3 you will build one more page.

Put your web page on the Internet

Put your page on the Internet.

Test your page

Validate your page before trying to get the JavaScript to work. Then make sure the JavaScript works.

Complete lab 3

You have done all the work for lab 3.
Now, send note to instructor.

Reading assignment

Reading assignments are in the text book, Java Script, A Beginner's Guide, Second Edition, by John Pollock; McGraw Hill / Osborne, ISBN 0-07-222790-7

Read Module 8.

I had the misfortune to teach out of a book, that was written by a community college teacher. They did not understand commercial programming, so they suggested approaches that worked in a school lab, but were bad in a large commercial project.
The author of this book seems to be an experienced web page developer, with a good working knowledge of commercial development using JavaScript. However, his background does not seem to be programming, so his descriptions of what the code is doing do not always use accepted programming terminology. So, I cannot resist suggesting several changes, which you may wish to mark in your book.

Read section 8.1 - In "What Is an Object", first paragraph, the book suggests you visual something general when thinking of an object. I suggest you think of something specific. An object in JavaScript represents a single specific thing. The book is a little fuzzy on this.

Read section 8.2 - In the section on "Constructor Functions", the book indicates   this   represents the current object; I would say it refers to the current object. Similarly, each variable, such as work_car, also refers to an object.
The book starts the name of Constructor Functions and Methods with a lower case letter. I suggest you start the name of Constructor Functions and Methods with an upper case letter. This distinguishes the constructors and methods from global function names and property names, which usually start with a lower case letter.
In the section "Putting the Pieces Together", the book refers to "two instances of the car object". This is very confusing. Instead, I would say, "The following code uses two objects. They have the same properties, because both were created by the Car constructor function.
In the "Putting the Pieces Together" section, the book makes two very good, important statements about organizing code:

The part of the script with the constructor function, instance creations, and variable assignments is placed in the HEAD section so that they load first
The document.write() commands are used in the BODY section so that they display in the browser

In the section on "Object Initializers", the keyword var is omitted before the variable names: work_car and fun_car. It actually works; you can omit the var keyword when you specify variables, and it usually works. I recommend you always use the var keyword.
In the first sentence of the section "Adding Methods", omit the word "call".
In the "Adding Methods" section, after the CAUTION, the next sentence should be:
"After you have defined the function, you need to save a reference to the function in a property of your object. This is often done in the constructor."
Still within the "Adding Methods" section, near the bottom of the page following the CAUTION, the sentence: "Also notice that when the function is called here, the parentheses are not used on the end of the function call." should be replaced by: "Also notice that a reference to the function is saved in the payment property. A reference to a function is provided when you write the name of the function without parentheses following the name of the function."
Ignore the "Ask the Expert" section.

Look at section 8.3 - This section discusses the object that represents the browser. Note that the browser is always called Navigator. Navigator was the leading browser when this object was built. The Navigator object is used for all browsers today. This stuff is very interesting, and often useful. We will mostly omit the Navigator; we cannot do everything in one course. We will look at other built-in objects, some of which we will use.

Alternate reading assignments are in the text book, Java Script Concepts & Techniques Programming Interactive Web Sites, by Tina Spain McDuffie; Franklin, Beedle & Associates, ISBN 1-887902-45-7

Read Chapter 16.

Lecture notes

Do NOT read the lecture notes before hearing the lecture. If you do, you will find the lecture very boring. Read the lecture notes if you do not attend the lecture, or if you wish to review the material.