Main Page

Requirements of object-oriented languages

The object definition is contained within a single function called a
constructor
. The constructor isn’t a
special kind of function; it’s just a regular function that is used to create an object. Later in this chapter,
you learn how to create your own constructors.
Requirements of object-oriented languages
Before a language can be called object-oriented, it must provide four basic capabilities to developers:
1.
Encapsulation
— the capability to store related information, whether data or methods, together
in an object
2.
Aggregation
— the capability to store one object inside of another object
3.
Inheritance
— the capability of a class to rely upon another class (or number of classes) for some
of its properties and methods
4.
Polymorphism
— the capability to write one function or method that works in a variety of differ-
ent ways
ECMAScript supports all four of these requirements and so is considered to be object-oriented.
Composition of an object
In ECMAScript, objects are composed of
attributes
, which are either primitive or reference values. If an
attribute contains a function, it is considered to be a
method
of the object; otherwise, the attribute is con-
sidered a
property
.
Working with Objects
The previous chapter touched briefly on how to work with objects, but now it’s time to go into more
detail. Objects are created and destroyed throughout the execution of JavaScript code, and understand-
ing the implications of this paradigm is vital to your understanding of the language as a whole.
Declaration and instantiation
Objects are created by using the
new
keyword followed by the name of the class you wish to instantiate:
var oObject = new Object();
var oStringObject = new String();
The first line creates a new instance of
Object
and stores it in the variable
oObject
; the second line cre-
ates a new instance of
String
and stores it in the variable
oStringObject
. The parentheses aren’t
required when the constructor doesn’t require arguments, so these two lines could be rewritten as
follows:
var oObject = new Object;
var oStringObject = new String;
68
Chapter 3
06_579088 ch03.qxd 3/28/05 11:36 AM Page 68


JavaScript EditorFree JavaScript Editor     Ajax Editor


©

брус клееный цена Киев.