Saturday, December 6, 2014

Object Oriented Programming

In OOP, think of an object as an abstract structure built out of properties and methods. Using this paradigm, the object is provided a scope by which to define its API. This also lends the object the opportunity to inherit from other objects and override existing implementation. Objects in javascript adopt the following form:

var Calculator = {
  name: 'Calculator',
  add: function(a, b) {
    return a + b;

Properties and Methods

As you can see in the example above, the Calculator object contains a public name property and a public add method. Properties are also known as the "state" of an object and methods, the "behavior".


Like all objects, our Calculator object has its own scope, which means you access it's properties and methods through the object itself, rather than globally like you would in a procedural language. To invoke the add function, you would simply call Calculator.add(1,2), and it would return 3.



  • Code is stored in objects in the form of properties and methods
  • Each object can have its own private and public scope
  • Objects are able to call methods of other objects (composition)
  • Objects are able to inherit properties and methods from other objects (inheritance)
  • Promotes code reuse
  • Dependencies are reduced to a minimum


  • OOP requires more overhead than procedural
  • Can be overkill for small applications
  • Must be extra careful when deciding between inheritance and composition or it will cost you unecessary overhead

That about wraps it up for this article. If you're interested in languages that have made the jump to a more OOP style, check out the transition between PHP4 and PHP5. Also, if you'd like to know more about inheritance in JavaScript, take a look at this article about prototypal inheritance and the use of call and apply as well. Also make sure to check out this article on procedural programming if you're curious about other programming paradigms.

No comments:

Post a Comment