Object-Oriented Technology in ECMAScript

Object-Oriented Terminology

Object

ECMA-262 defines an object (object) as 'an unordered collection of properties, each property holding a primitive value, object, or function.' Strictly speaking, this means that objects are an array of values without a specific order.

Although ECMAScript defines objects in this way, its more general definition is based on the representation of code nouns (people, places, or things).

Class

Each object is defined by a class, and we can think of a class as an object recipe. A class not only defines the interface (interface) of the object (the properties and methods accessed by the developer) but also defines the internal workings (the code that makes properties and methods work). Compilers and interpreters build objects based on the class description.

Instance

When a program uses a class to create an object, the generated object is called an instance of the class. The only limit to the number of objects generated by a class comes from the physical memory of the machine running the code. Each instance behaves the same, but each instance handles a set of independent data. The process of creating object instances from a class is called instantiation.

As mentioned in the previous chapters, ECMAScript does not have formal classes. Instead, ECMA-262 describes objects as object recipes. This is a logical compromise in ECMAScript because object definitions are actually objects themselves. Even though classes do not truly exist, we still call object definitions classes because most developers are more familiar with this term, and functionally, they are equivalent.

Requirements of Object-Oriented Languages

An object-oriented language needs to provide developers with four basic capabilities:

  1. Encapsulation - The ability to store related information (whether data or methods) within an object
  2. Aggregation - The ability to store one object within another object
  3. Inheritance - The ability to acquire properties and methods from another class (or multiple classes)
  4. Polymorphism - The ability to write functions or methods that can run in multiple ways

ECMAScript supports these requirements and can be considered object-oriented.

Composition of Objects

In ECMAScript, objects are composed of attributes, which can be primitive values or reference values. If the attribute holds a function, it is considered an object method, otherwise, the attribute is considered an object property.