Object-Oriented Programming Basics

 

Object oriented programming languages have a number of special features which you need to understand fully in order to get the most out of these languages. The object structure is a very powerful and useful one once you have learned how to use it.

The following is generally true of both Javascript and Actionscript. In the text below, when referring to Javascript, I mean BOTH Javascript and Actionscript.

 

Objects and Properties

As we previously stated, objects are basically a collection of related values called properties. What we didn't say was that properties can also themselves be objects, so that there is a hierarchical settup possible of objects within objects.

One good explanation of this system is to imagine a real-world metaphor. You body contains many parts - in representing these in Javascript, you might start by making a mybody object, which contains properties such as leftArm, rightLeg, head, torso, etc. One important thing to understand right away that individual objects of a certain type are predefined by the browser or may also be defined by the programmer. These object types function as templates for a certain kind of object. One then creates one ore more instances of this object. For example, in our body scenario, there might be an arm object type declared, with 2 instances of this object type being created: leftArm and rightArm.

In object syntax, we would then refer to our left arm as:

mybody.leftArm

and then, assuming that the arm object type had a property for the hand, and that this was an object with a property for each finger, then we would refer to our first finger as:

mybody.leftArm.hand.firstFinger

and our thumb as:

mybody.leftArm.hand.thumb

Taking this a bit further,

mybody.leftArm.hand.thumb.nail

would then refer to the nail on the thumb on our left hand.

 

In fact, in a web browser, the document is represented by the document object, which contains properties for all the possible elements in the page. This includes an images object, which contains a list of the images in the page, a layers object which contains all the names of the layers, as well as other elements of the document which you might like to manipulate using Javascript. Often, the hardest part of writing Javascript code is remembering the precise path to a specific property you need to know the value of, or would like to change.

 

Methods

 

Often a particular object will know how to do various things. Learning how to define this capability will come up much later. For right now, all you need to know that many objects which already exist have what are called methods. These methods are specific to each object, and each of these methods causes the object to do something. For example, let's say that in our body example above, there existed a "wiggle" method for each finger, which would cause it to wiggle. The syntax in general for a method is thus:

methodName(optionalArgument1,optionalArgument2)

with the arguments being optional depending on the particular method. In the case of our example, if we wanted to wiggle the first finger on the left hand, we would say:

 

myBody.leftArm.hand.firstFinger.wiggle( );

 

to wiggle the third finger on the right hand, we would say:

 

myBody.rightArm.hand.thirdFinger.wiggle( );