Professional JavaScript | 4 | WebReference

Professional JavaScript | 4

123
[previous]

Professional JavaScript

Polymorphism / Late-Binding

Polymorphism and late-binding are concepts designed to make computer objects a little like real-world objects, by ensuring that using common behavior doesn't require an in-depth knowledge of the object. An example from the real world is picking up and throwing things. Most small things, like pebbles, sticks, fruit, books, eggs and shoes can all be picked up and thrown, and you don't need to know too much about them in order to get them to sail through the air. You certainly don't need to know how many pips the fruit has, or whether the shoelaces on the shoes are nicely done up. You may have to modify your behavior a bit for some less common throwable objects, like houses, cats and loved ones but the principle is there – the details don't matter too much.

For strongly typed languages such as C++ and Java, acquiring this kind of "don't care" behavior is not trivial because the nature of all available objects is supposed to be strictly and completely known at all times. For those languages, the problem is solved with the technique of late-binding which, very roughly speaking, defers the "need to know everything" requirement until a given program is actually running. Such languages normally need to know everything at compile (preparation) time.

In the JavaScript case, the language is designed from scratch so that you don't have to care:

var thingo = new Object; ... // do anything with thingo thingo.gently_lob();

In JavaScript, everything is late-bound, because the language is interpreted – decisions about whether methods or properties exist are made at the last second. Secondly, JavaScript objects are loosely-typed, so the language never had a need to rigidly understand objects in the first place. In the example, it doesn't matter where thingo comes from, or what type it is, you can easily try to gently_lob() it, and the worst that will happen is that you'll get an error saying "no gently_lob() method". If a gently_lob() method exists, it'll be called without fuss, as you'd expect.

From this example you can see that JavaScript has very straightforward support from polymorphism.

Association

Association is not a core requirement for object-orientedness in a language, but it is used so much in object-oriented design that it's worth covering for completeness. Association is a powerful concept because it is the only way that objects can have access to each other without the "sole ownership" implications of containment as discussed above.

Because any JavaScript variable can track any object, association is implemented very naturally in the language. A simple example:

// Object separate to other concerns var car = new Mini(); car.capacity = 4; car.passengers = 0; // Second as-yet unrelated object var person = new Traveller(); if ( car.passengers

In this example, many Traveller objects might use the same Mini object as their preferred transport. Hopefully checks will always be done so that sharing the object makes sense. In this case that means that no more than four Traveller objects should share the Mini object. When a Traveller object is finished with the Mini object, then this code might be used:

delete person.transport; or alternatively person.transport = null;

When this happens, the Mini object won't cease to exist, as the car variable will always point to it. But one less person will associate the Mini object with themselves.

For association, common language is to say: the Traveller object uses a Mini object. This is sometimes more formally written: USES-A. Because JavaScript provides no mechanism to distinguish between containment and association, it's up to the scriptwriter to put his design hat on and stay clear in his own mind which use is intended.

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

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