Monday, June 10, 2013

Javascript Object Oriented Fundamentals

Before I can answer the question about  how object oriented is Javascript, maybe I need to dip my toe into the fundamentals of object orientation first. For something to be an object, it must combine data and behaviour into a coherent whole. Yes, it is as simple as that, but experience has shown me that simplicity is always more difficult than complexity. An object provides crisp abstractions with clear boundaries. Objects also support reusability through inheritance and/or composition of behaviour and data.

So Javascript is most certainly object oriented, but in my humble opinion, it's implementation is messy. The syntax and organisation have a very procedural feel about it, although it is not really possible to write procedurally, since all data and functions are combined within a global object

Javascript implements inheritance using prototype objects. Every object contains a prototype object and when a message is received by an object, Javascript will look for the property/function in the object first, then look for it in it's prototype object, if still not found it will look at the prototype's prototype object till it ultimately gets to the global object's prototype object. I will delve deeper into this in a later blog.

Last but not least Javascript also has constructors which are implemented as functions, and they can be implicitly or explicitly defined.

Enough on object orientation for now, till next time when we look at the few primitives that pollute the Javascript universe.....

See Part 2 - Objects, objects everywhere                           Part 4: JavaScript Primitives, The Imposters!




No comments: