An Inroduction to Object Oriented Programming


For a list of the examples done in class Click here

Definitions

Classes
Template for creating objects
Objects
"Things" in the world. An object consists of methods and properties.
Methods (aka actions)
What objects are able to do.
Porperties (aka member variables or data)
The information that objects store about themselves..
Messages (call to a method)
Requests sent to objects to perform actions. See notation below.
Parameters
data that is sent with the message that the object needs to perform the action.
Return Values
Once the object has performed the action, it may send information back to the object that sent the message. This information is called the return value
Events
Actions performed by the user such as button pushes, key presses, etc.
Event Handlers
Javascript commands that are triggered by events. (e.g. onClick)

Notation for sending a message.

Messages consists of: FullObjectName . methodName (parameters)

The object name refers to the object receiving the message. The name must include the name of the object itself as well as all objects that contain it. The method must be a method that the object contains. Note: Methods always end with parentheses whether or not they have parameters. For example: move(1,2), or jump(). Object names never end in parenthees: For example: fish1.tail or oven.door.handle.

Properties and Methods

Objects created from the same class can do the same actions. They also contain the type of data, although the value of the data may be different:

fish class:

Has the following properties: color, size:

fish1: color=red, size=10

fish2: color=blue, size = 12

both can do the actions: swim, turn

table class:

Has the following properties: width, height, length

table1: width=10, height=5, length=20

table2: width=20, height=15, length=25

both can do the action: place, scale

Objects in JavaScript

When a browser reads and displays an html document the browser will create many javascipt objects. For a listing see Appendix A. Also contained in this appendix is a listing of all the objects with their corresponding properties, methods, and event handlers.

Writing JavaScript

Javascript can be placed in the head or the body section. Which is best depends on the script. Scripts are specified with the script tag:

    <script language=javascript type="text/javascript">
       document.write("This is a Javascript example!")
    </script>  

Values, Variables, Assignments, and Comparisons

Types of information:

In order to manipulate information and actions, all information and actions are referred to by names. We have already seen that objects and methods have names. Properties also have names.

Variables: variables are named "slots" for storing data. For example, if we say that age = 18, the word "age" is the name of the slot where we store the value of 18. Whenever we use the name "age" we will be referring to the value 18. If we later say age=21, then the value of 18 gets replaced by the value 21.and not "age" will refer to the value 21.

Types of variables: number, string, boolean

Operations: Symbols used to manipulate variables. For example if we have the variables:

width=10, height=20

then we can compute the value of a variable called area as follows:

area = width*height

The value of the variable area will now be 200. See page 8 for other operators.

Assignment Statements: In general an assignment statement computes the value of the RIGHT side of the "=" sign and places the result into the variable name on the LEFT side.

age=21 is said to assign the value of 21 to the variable age.

The right and left hand sides of the equal sign are not equivalent:

21 = age     NOT A LEGAL ASSIGNMENT STATEMENT!!

age+5 = age     NOT A LEGAL ASSIGNMENT STATEMENT!!

area = width*height     LEGAL ASSIGNMENT STATEMENT!!
  Says to take the values stored in width and height, multiply them, and place th result into the slot called area.

Assignment statements are NOT the same as mathematical equations even though they look the same. For example:

age = age + 5     LEGAL ASSIGNMENT STATEMENT!!

This is not a legal mathematical equation. However, it makes perfect sense as an assignment statement. It says the take the current value of age, add 5 to it, and store the result back in the slot called age.

Comparisons: Often we only want to do things only if certain conditions hold. For examp[le: I will go outside if it is not raining. These are called conditional (or boolean) statements. In Javascript we express conditional statements using conditional (or boolean) operators:

x > y
This has a value of true of x is larger than y. Otherwise it has a value of false.

Boolean variables are variables that contain the value true or false.

bigger = x>y
Here, bigger is a boolean variable. If x is 10 and y is 20, then bigger will have a value of false.

See p. 9 for the boolean operators.