Tuesday 24 June 2008

Getting intimate with Javascript objects part #4 - Orienting your Objects

Ah Hah! Now the going gets interesting - Object-Oriented Javascript!

You would think that a language that doesn't have a formal class would have it hard playing with the classic object-oriented languages like C++, Java and (lord help me) C#. Object-Oriented Geeks can be a funny lot and they would have you believe that a "real" object oriented language is strongly typed and supports class based inheritance. As an example of this kind of delusion I give you Ben Poole who spends all day ferkling with java in eclipse and is proud to wave goodbye to the last remnants of his street cred by calmly stating in broad daylight and SOBER, that Kajagoogoo are cool ... need I say more? (actually Ben is a lovely chap who has no problem with javascript but he does spend WAY to long Ferkling!)

As I have shown in the last couple of posts. Javascript makes use of objects, has its own prototype based inheritance and as a result can stand up proud as a OO language along side Java (no relation) and C++. If the other Object Oriented languages want to look down their noses at Javascript well let them, they are no better than they should be anyway!

OK for those of you that are unfamiliar with Object Oriented terminology (I couldnt be arsed typing Object-Oriented all the time so it will from now on be shorted to OO) here is a quick summary. An Object is a data structure that contains various bits of named data called properties and may contain various functions called methods that do things.

Objects are our friends, love them, hug them and call them George.

Why? Because they group related data and functions into a convenient package which naturally increases modularity and resuability of code. YIPPEE! Being a professionally lazy person I don't like working hard and constantly writing the same bit of code, to be frank, skunders my pish, anything that means I have to type less is an A+, Gold Star, goto the top of the class sort of thing to me! Not that has anything to do with OO programming of course thats just me being lazy.

In Javascript we can have any number of methods and properties and dynamically add them to our objects basically when we need them. In Java and C++ because they are "strictly typed" you can't do this (well not easily) that's why they are called strict. Also each object has a predefined set of properties (called fields) and each property has a predefined type. These definitions are made in the CLASS an object in Java or C++ is defined by its CLASS a bit like the late 1950's in England sorta like this ...waits for the video to be consumed... whilst javascript does not have a rigid class structure it can do most of the things the "proper" OO languages can so we will not get a sore in our neck looking up at Java and C++!

In any OO language there may be many objects created from the same class. When often say that an object is an instance of the class and if we are being really geeky we don't create an object we instantiate it. So if you don't want to be laughed at but ubergeeks don't say "I have just made a HarveyWallBanger doobrydo from my Cocktail thingy" say "I have instantiated a HarveyWallBanger object from my Cocktail class" it will make you look way cool!

In Java it is a common convention to name classes with a capital letter and to name objects with lowercase letters. This helps keep classes and objects distinct in code. This is a useful thing to do in Javascript as well.

That's enough for today.. tomorrow I will continue with how Javascript copes with the idea of Instance Properties,Instance Methods,Class properties and Class methods.

Disqus for Domi-No-Yes-Maybe