Credit: Kirk Born (St Sci/NASA)
JavaScript as an Object Oriented Language
JavaScript is very much an Object Oriented (OO) type language. The main
goals of OO programming 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,"premajor",...)
}
for (i = 0; i < n_Staff; i++ ) {
staff[i]=new Staff("temporary",...) {
}
for (i = 0; i < n_Faculty; i++) {
faculty[i]=new Faculty("tenured",...) {
}
The parameters you pass to the functions that define the class
supply some of the values that define each 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.
Some Major Concepts in OO Programming
- The keyword this is both a very important and
somewhat difficult concept to grasp. It is used to identify the
components, such as values and functions, that belong to one specific
instance of a class of objects. For example, suppose that we have a
class called "Patient" which contains a number of variables and functions,
for example:
function Patient() {
var age, weight, sex, ...
function bloodSugar() { ... }
function bloodPressure(0 { ... }
...
}
We can generate many "instances" of this class using the syntax
for (i=0; i <N; i++) {p[i] = new Patient() } .
Now suppose that we have accessed a specific patient, p[k], and that one
of its internal functions, say "bloodSugar()"
calls some other function and passes it the value this.
Then that other function can access the exact values and functions that
belong only to that specific patient, p[k].
- Prototypes and inheritance. Some languages, such as
Java and C++, let you create subclasses which inherit the values
and functions of their higher level classes. JavaScript implements a
similar scheme called a prototypes which define
values and functions which all "instances" of that class will inherit
using the syntax
Class.prototype.value = value or
Class.prototype.function = function
- Scope Chains. The scope of a variable or function
is the object which it belongs to. For example, if you use the
keyword var to define a variable inside a function,
then that variable has a meaning and value that belongs only to that
function, and any variable with the same name but located somewhere else
in the program is a totally different value.
Since functions may be nested to any level, there arises a
"scope chain" in
which variables or functions defined with the var keyword
are uniquely defined at their level. It turns out that their are two
types of "scope chains" in JavaScript: static versus dynamic scope
chains. Static scope chains are defined by the nesting you create for
functions, whereas dynamic scope chains are built into the HTML
hierarchy to which they belong (although you may explicitly override
some of the values or functions in that hierarchical chain).
See
Examples of Object Properties
for many examples of the concepts presented here.
|
|
 |
|
 |