Thursday, June 13, 2013

JavaScript Primitives, The Imposters!!!

Allegedly the JavaScript universe contains 5 primitive types:
  • string - a sequence of unicode characters
  • boolean - represents a truth value of true or false
  • number - represents any number, even really big ones, between -253 and 253
  • undefined - indicates that a property or variable is unknown, does not exist
  • null - indicates no value
I say these primitives are imposters! They behave like objects but are not. JavaScript being a loosely typed language morphs the primitives into objects when used as objects. These primitives also have object wrappers.

var primitiveString = 'I am a primitive string';
var objectString = new String('I am a String object');  //using the object wrapper

primitiveString.length;   // 23, will work and return the correct value like an object


You could even do this

primitiveString.myvalue = 'test'     //like an object

which assigns the value 'test' to a hidden object property that you have no reference to, so you can't access the property from the primitive type thereafter.

null and undefined are also very strange, since they are just properties of the global object and can be redefined by you, but please, don't do that. More on this later.

Whether you use objects or their imposters, doesn't matter, just be consistent, so as to avoid hard to find bugs. Next, I play with JavaScript's awesome arrays, until then, may the code be with you.......

Part 3: JavaScript Object Oriented Fundamentals

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!