Professional JavaScript | 2 | WebReference

Professional JavaScript | 2

123
[next]

Professional JavaScript

Is JavaScript Object-Oriented?

Programming languages that are object-oriented are considered to have powerful features above and beyond other, merely mortal languages. Moreover, the style in which developers must think when working with object-oriented languages differs from that of more traditional procedural languages like Pascal and C.

There's some debate about just exactly what features are required to make a language object-oriented. From a practical point of view though, these four features are probably enough to draw a Yes vote from most people:

Just for completeness, we should really add a fifth item to our small list. However this one is more of a consequence of the other four rather than a fifth necessary feature of object-oriented programming.

We'll look at each of these points in a minute, but to cut to the chase JavaScript scores highly on all fronts:

Inheritance is a crucial concept for object-orientedness. Without inheritance, or so-called "robust support" for inheritance (meaning enough proper features built into the language), JavaScript could only be said to be object-based, not object-oriented. Object-based languages miss out on a convenient fit with some powerful software modeling techniques such as Unified Modeling Language (UML). Strongly typed languages such as C++, Java or ADA might take such a view of JavaScript. However, other interpreted languages such as Smalltalk and Perl would probably agree with JavaScript, so it comes down to picking sides. Pragmatically, you get some language assistance with JavaScript for inheritance, so that's probably enough to qualify.

But the key question is simple. Why would you want or need to start writing JavaScript scripts in an object-oriented fashion? JavaScript is probably at its best when it leaves the complex object handling stuff to the host software that the interpreter is embedded in. Your script can thus stay short and sweet and achieve powerful effects through the host objects exposed with no real need for an OO thought in your head.

Consider the point down the line however when your script starts to collect dozens of custom-made JavaScript objects. Relying on JavaScript's relaxed syntax at this stage to whip together a quick and dirty script is not going to work for you much longer and you will begin to need a more planned and designed approach. JavaScript centers around objects, therefore an object-oriented design is a natural and sane fit.

JavaScript Support for OO Concepts

As you walk down OO Programming Parkway, you'll realize a need to understand how to implement the tenets of this modus operandi in actual code, so in the next few pages we'll go through how each of the five important object-oriented concepts mentioned above fit into JavaScript.

Encapsulation

You'll remember from Chapter 2 that in JavaScript, pretty much everything is the property of an object (if it's not a method). Now take the following example.

function area() { return this.dimension * this.dimension; } function Square(size) { this.dimension = size; this.area = size * size; } var object1 = new Square(4); // make one var object2 = new Object; object2.dimension = 4; object2.area = 16; // make another

It's pretty obvious that all the script does is to create two effectively identical objects. They both have the same properties and the same values associated with those properties. So what we can see from here is that ignoring how they got those properties, both objects group the two together under one umbrella which we can refer to – they encapsulate the properties.

In this example, object1 is probably created in a more "object-like" manner since we bother to define all the features of the object in one place (in the constructor function Square()) before we make the object up. Functionally there is little difference.

In other object-oriented languages, information hiding goes hand-in-hand with encapsulation. This means that not only are the candidate data and functions collected together in one object, but also restrictions are placed on how the items can be extracted or accessed afterwards. Typically it is the candidate data items that are heavily restricted. In JavaScript, once the above objects are created there's nothing stopping you from getting at the dimension property thus:

var total = object1.dimension + object2.dimension; object2.dimension = total; If JavaScript had heavy restrictions you might have to create special methods to access the dimension property (after adding other special magic to hide it in the first place): var total = object1.getSize() + object2.getSize(); object2.putSize(total);

You can still do this (except for the special magic required to do the hiding), but there's not much point. The solution for JavaScript is to relax – it's not that important. Access the property directly – the simple nature of JavaScript is designed to encourage a non-rigid approach.

123
[next]
and
Created: February 5, 2001
Revised: February 5, 2001

URL: http://webreference.com/programming/javascript/professional/chap3/