1. Home
  2. Computing & Technology
  3. JavaScript

Object Oriented JavaScript
2. The Benefits of OO JavaScript

By , About.com Guide

Before we start to look at how to write Object Oriented JavaScript let's first consider why we might want to actually write our JavaScript that way rather than using the somewhat simpler coding techniques that many people use with their JavaScript.

Object Oriented Coding offers a number of differences from alternative coding techniques. Two of these are abstraction and encapsulation. What this means is that once we have created an object we can interface with that object entirely using its public methods and properties without any regard for how the object does what it does. The object effectively becomes a part of our particular expanded version of JavaScript. Most JavaScript libraries that you can obtain to make your JavaScript coding easier all use object oriented JavaScript within the library itself in order to make it easy for people to perform the tasks that the library is designed to provide. Abstraction means that once you start using a library of objects designed to perform given tasks you no longer need to worry about exactly how to perform those tasks without the library. You simply call the appropriate methods for the appropriate objects and the library does the rest. The code within the objects in the library is encapsulated so that the exact way in which they implement that functionality doesn't affect the way you call it. The library authors can rewite code within the objects in their library and provided that they don't change the public interface you don't need to change any of your code to use their modified objects.

Using object oriented coding makes your code more modular and less likely that different sections of code will clash with one another. Where you define properly encapsulated objects the only way they can clash with other code is where the other code gives something the same name as your object. Where you decide to use a library with your JavaScript code the things within the library that are most likely to clash with other code are those parts of the library which do not use object oriented coding.

Object Oriented programming allows you to build on what you already have defined when creating new objects that have a hierarchical relationship to one another. When setting up the code for a new object you do not have to start from scratch in defining the object if you already have another object that defines a part of the required processing. You can simply base your new object on the old one and add the additional functionality that the old object doesn't already provide. JavaScript allows you to take this one step further in that you can add new properties and methods to existing objects at any time and they will be instantly available both to that object and to all objects that have beed derived from that object.It is not recommended to add new properties or methods to the built in Object object since almost all objects are derived from that one and therefore it has the potential to break other code that rely on the way that the Object object is defined.

A lot of the way that your JavaScript will interact with the web page will make use of objects that are already built into the browser. You will not necessarily need to add your own objects in your code in order to achieve simple goals. With many short JavaScripts you will just need a few lines of code that reference existing objects in order to perform the required processing. What I have found is that where I have written a script without using object oriented techniques within my own code in order to add some more involved processing to a web page that as soon as the situation arises where I want to add two such pieces of code to the same page that the simplest way to achieve the desired result is to rewrite the code using my own objects. For anything that involves more than a handful of lines of JavaScript the benefit of spending the small amount of additional time to write it in an object oriented fashion in the first place will result in huge time savings afterwards. In any case it is just about impossible to avoid objects in JavaScript since even of you don't create your own you will still need to interact with the ones that are built in.

All the Object Oriented JavaScript Tutorials

  1. What is Object Oriented JavaScript?
  2. The Benefits of OO JavaScript
  3. JavaScript's Built in Objects
  4. Extending Built In Objects
  5. Creating Objects from Existing Objects
  6. Creating New Objects Without Copying Existing Ones
  7. Dot Notation and "this"
  8. prototype
  9. Inheritance and "constructor"
  10. Associative Array Notation
  11. Create Method if Doesn't Exist
  12. "self" and Multiple Objects
  13. Defining Objects with JSON
  14. Namespaces
  15. Lazy Definition
  16. Extending Methods
  17. Copying Objects
  18. Private Properties and Privileged Methods
  19. Public Access to Private Methods
  20. Chaining Methods
  21. Singletons
Explore JavaScript
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. JavaScript
  4. Javascript Tutorials
  5. Object Oriented JavaScript
  6. The Benefits of OO JavaScript>

©2010 About.com, a part of The New York Times Company.

All rights reserved.