Credit: Samuel Zschokke
The most frequent languages used on the web are JavaScript, JScript,
VBScript, and Java on the client side, and Perl, Python, Rexx, C, and C++
on the server side. The client side languages all have counterparts on
the server side as well, but we will consider only their client side
implementations. The main division lines among languages that we will
consider are:
- Compiled languages
- Interpreted languages
- Object Oriented languages
Which is better? Compiled languages are usually much faster in execution
(except Java which is about 10 times slower than C because it has the
intermediate step of virtual machine-to-real machine translation). They
also provide
very powerful syntax checking capabilities which let you detect many errors
at compile time which can be much, much easier to debug
than runtime errors. So they are best for large programs which process
lots of data, and especially for programs designed by teams of people.
Interpreted languages are often easier to write as you
don't have to pre-specify data types in advance and you can fling any data
at it with aplomb (check out
this for examples of the flexibility of JavaScript).
However, they are slower and don't provide the
powerful syntax checking capabilities of compiler languages and so are
better suited to small programs created by one person where runtime
errors are easier to find.
Object Oriented Languages
The best known Object Oriented Languages (OOL)
are JavaScript, JScript, VBscript, Java, and C++, although Perl 5 features
OOL properties, as do newer versions of REXX and Python.
Object oriented languages are a whole new programming paradigm.
Instead of concentrating on the "processes" that are to be performed, you
concentrate on the "classes" and "classes-of-objects" that make up
the entities that are manipulated by your
program. The main goals of Object Oriented Programming (OOP) are to:
- Identify classes that group similar entities together
- Create objects from the classes, using the
new Class()
syntax
- Populate the classes and objects with variables and functions
(called "methods") which describe their behavior
The following analogy might be helpful. Consider a large sheet of
dough, and suppose you have a number of cookie cutters in various shapes,
such as a Christmas tree, Donald Duck, a GingerBread man, and so on. Each
cookie cutter represents a class, and each time you stamp out a shape with
the cookie cutter you generate a new object of that class. All of the
objects in a class (all the pieces of dough stamped out with the same
cookie cutter) have similar properties, in that they share the shape
of the cookie cutter, but might also have individual properties, such as
green sprinkles versus blue sprinkles. So objects in a class share certain
major characteristics in common, but allow for individual variation as
well.
You determine the classes you use based on your purposes.
For example, suppose you are developing a web-based system to keep track
of people in your department, and suppose, for the purposes of the
program,
that students, staff, and
faculty must be treated in very different ways. Then it makes sense to
setup your classes to reflect this natural division, as follows:
// Define the classes----------------
function Student(year,major,..) {
...
}
function Staff(classification, ...) {
...
}
function Faculty(tenure, research,...) {
...
}
...
// Create the objects for these classes -
for (i = 0; i < n_Students; i++) {
student[i]=new Student(2,class[i],...)
}
for (i = 0; i < n_Staff; i++ ) {
staff[i]=new Staff(pos[i],...) {
}
for (i = 0; i < n_Faculty; i++) {
faculty[i]=new Faculty(type[i],...) {
}
The parameters you pass to the functions that define the class
supply some of the values that define the object. In addition,
you may add functions and other variables both to the classes and to
individual objects to further describe their behavior.
On the other hand, you may decide that, for you specific purposes,
staff and faculty should be treated alike, whereas students
are to be treated differently. In that case, your division might be
FacStaff versus Student.
Procedure oriented programmers may have a difficult time making the
transition to OOP, especially as OOP usually takes more time in the
design phase, and the solutions are often more general then they need be.
But the code is usually much more reusable, robust, and easier to modify
and extend.
Object Oriented languages are particularly important for the WWW because
its very structure lends itself well to that approach.
Here is a very simplified outline of
the structure of a web page:
- Window: the top of the hierarchy
- navigator: contains information on the browser and the client
- event: contains information on mouse/other events
- frames: these are declared with the "frameset" HTML code
- document: contains the main content of a window
- all: in IE4 this accesses all style sheet elements
- body: the main body of the document
- forms: container for all forms elements
elements: text, buttons, select-one, etc
- images: all images declared with <IMG SRC...>
Most of the operations on HTML elements make use of this hierarchical
structure where each element that is manipulated, be it window, document,
form, image, button, or text box is an object that belongs to a class.
See this
for an example of generating and manipulating simple objects, in this
case a user-defined object named "Rect", and a window and two element
objects. The "Rect" object has two data attributes "height" and "width"
and two "method" (function) objects named "area" and "perim": here
is a picture of the "Rect" object.
See this
for a much more complex example of how objects are
defined and used. In this case, each visible icon on the screen is an
object which has both data and methods (functions) defined for it.