Over the years, I have used something called the "object-oriented car", a common metaphor in programming instruction, to explain object-oriented programming principles. As I have grown more sophisticated in my explanations, however, the "car" object example has begun to break down. Now, instead of describing the entire car in my lectures, I describe only the STEERING WHEEL of the car using object-oriented programming terms.
Let us create a SteeringWheel object (and notice how the name of the object has NO SPACES!). This SteeringWheel object has one property, "direction", and one method, "honk()".
The direction property of the SteeringWheel object says what direction the SteeringWheel is pointed. Since this is a VERY simple SteeringWheel, only left, right, or center would be legal values for this property.
The honk() method of the SteeringWheel object honks the car's horn; we'll talk more about this method later on.
The SteeringWheel constructor merely defines the basic structure of the SteeringWheel object; it does NOT actually create a SteeringWheel which you can manipulate directly. To get a SteeringWheel object that you can use, you need to create an INSTANCE of the SteeringWheel object (also known as INSTANCIATING a SteeringWheel object). When you create an instance of an object, you are creating ONE copy of the object which you have control over.
var theWheel = new SteeringWheel();
Whenever you instanciate an object, you MUST use the keyword, new, before the call to the object constructor, as you see above. Also, notice the opening/closing parentheses characters following the call to the SteeringWheel constructor (and prior to the semi-colon end-of-line marker); these parentheses are called the "function call" operator (which calls/invokes a function), and are an essential part of the object instanciation process.
Here's the code again:
var theWheel = new SteeringWheel();
Once you have created an instance of your desired object, you may access the PROPERTIES and METHODS of the object by using DOT SYNTAX.
Dot syntax is a way of accessing child elements of a parent object (remember parents and children?) by using period/dot (.) characters between parent and child elements to indicate a path through a complex tree of relationships, much as the slash (/) character is used in URL syntax to indicate a path through a complex directory structure on a web server.
earth.northAmerica.usa.california.sonomaCounty.guerneville.cinnabarCoffee.cashRegister = 10;
Dot syntax always creates a path from the most general to the most specific element, reading from left-to-right, and, as you can see from the above example, there are NO SPACES IN THE NAMES of the elements or between the dots!
Back to the instance of our SteeringWheel object, theWheel:
The SteeringWheel object has one property, direction. Object properties can be accessed via the instance of the object using dot syntax. In the following examples, I am assuming that theWheel has already been declared and instanciated.
theWheel.direction = "left";
In the above example, I have set the direction property of theWheel object equal to the string, "left". Surprise! An object property is really a VARIABLE which is attached to a particular object; this property may be filled with whatever data we like (in this case, a string).
Having executed the above line of code, theWheel is now pointed towards the left.
theWheel.direction = "right";
Now, theWheel is pointed towards the right.
Please cut me some slack, I'm trying to make a simple example here!
Now, if I wanted to honk the horn of theWheel, I would need to call the honk() method of theWheel.
A METHOD is really a FUNCTION which is attached to an object, just as a PROPERTY is really a VARIABLE attached to an object (again, a function is a program which performs some action)! Therefore, I use the function call operator (opening/closing parentheses) after the name of the method (and dot syntax, of course).
The above example executes the honk() method of theWheel, causing theWheel's horn to honk.
Example (complete code):
var theWheel = new SteeringWheel(); theWheel.direction = "left"; theWheel.direction = "right"; theWheel.honk();
Boy, if we drove like this down the road, we'd probably get a ticket for reckless driving!
You can create more than one instance of an object, simply by declaring and instanciating more copies of the object.
var theWheel = new SteeringWheel(); var theSecondWheel = new SteeringWheel(); theWheel.direction = "left"; theSecondWheel.direction = "center"; theWheel.honk();
In the above example, there are two copies of the SteeringWheel object (hopefully attached to two separate cars!). The direction property of theWheel is set to "left", while the direction property of theSecondWheel is set to "center". Then, I honked the horn of theWheel, but I left theSecondWheel silent.
Each instance of an object is separate, and contains separate information. Even though both objects in the above example are SteeringWheel objects, they are completely independant from one another!
I can make as many instances of an object as I want, but I think TWO SteeringWheel objects is enough for now...
Copyright © 2001 Michael Masumoto. All Rights Reserved.