Object Programming and Object-Oriented Programming
© 18 February, 2013, Martin Rinehart
Object-oriented programming (OOP), a set of techniques born in Simula and Smalltalk, became the mainstream paradigm after Stroustrup/Bell Labs introduced C++ (1983), a better C, specifically improved with the introduction of OOP. As C++ grew to become a dominant language (and a very large one, too) Java was introduced (1995). It adopted the C++ OOP model, substituting interfaces for multiple inheritance. Java grew to replace Cobol (1959!) as the dominant language for commercial data processing. The C++/Java OOP model, although it was arguably a step backward from Smalltalk, is now called "classical."
The essential difference between OOP and OP is that in the former the programmer works with the values of properties. In the latter, the programmer works with properties, both names and values, and with the objects themselves, adding and/or removing properties as needed.
In classical OOP, one begins with a module called a "constructor." The constructor defines the properties (names and types) that are part of a "class" of objects. Each member of the class, called an "instance" of the class, will have the same set of properties.
this within the constructor), assigns a reference to the constructor in the object's prototype, available as
this.constructor, and assigns
this.constructor.prototype as the prototype object of
this. As in OOP, the programmer writes code as needed to further equip
this with an initial set of properties. The constructor then returns the
this reference to the object.
In object programming, working with property values is done just as it is in classical OOP. You can write, for example:
foo.bar = expression;
That line evaluates the
expression and assigns the result to the
bar property of the
foo[ 'bar' ] = expression;
That does exactly the same thing, substituting subscript notation for classical dot notation. This gets interesting when you consider this:
var propname = name_expression;
foo[ propname ] = expression;
The above evaluates the
name_expression and uses it as the name of the property for which the next result will be the value. If the property exists, its value is replaced (as in OOP). If the property does not exist, it is created. (As is uncommon in OOP. Hybrid languages, such as Python, allow dynamic creation of object properties. This capability is fundamental to object programming.)
The page Object Programming Examples provides additional object programming examples. It shows how a very simple function can "add" two objects together (creating one object with the properties of both added objects). It shows how this can be used in styling DOM elements on an HTML page.
person[ 'name' ] = 'Martin'; // typical
// after reversing:
person[ 'Martin' ] // value is 'name'
The classical and prototypal inheritance models achieve the same goals. The programmer used to one,however, will need to do some fresh learning to master the other.
Object Programming Defined
The OOP model combines object instances, sets of key/value pairs, with a class structure that provides a routine to create instances, storage for instance methods and other services. This definition of object programming should provide significantly more power:
- An object programming (OP) system allows the creation, modification and disposal of objects.
- Objects are collections of properties. Properties are key/value pairs.
- "Modification" of objects means the ability to create, modify and dispose of properties. That includes modifying keys or values.
- An OP system also provides for the miscellaneous services provided by OOP classes, such as creating object instances and storing data and code at the class level (to avoid duplicating either in each instance object).
Feedback: MartinRinehart at gmail dot com
# # #