1. Computing

Object Oriented JavaScript

4. Extending Built In Objects

From , former About.com Guide

If we are looking at the easiest way to move gradually toward a more object oritnted approach toward programming with JavaScript then the next step beyond using the objects already built into JavaScript is to extend those objects to provide additional functionality by adding our own methods. Simply adding an extra method to an existing object is a much easier process than creating a whole new object and doing so clearly demonstrates how we can dynamically add new methods to any JavaScript object at any time, something which cannot be done with most other object oriented languages.

Not all of the built in objects are as easily extended as others. First of all one object that you should never extend by adding anything to it is the Object object which is the base object upon which all other objects are derived. Extending that object runs the risk of breaking code anywhere in JavaScript that relies on a knowledge of what proerties and methods the given object has since extensions to the Object object are effectively added to all objects.

Other objects which you will never need to extend include the Math object which is simply a repository for mathematical constants and functions, and the Boolean object which is basically a true/false flag.

Some built in objects are more obvious candidates to be extended than others. Some of those where obvious extensions exist include Date, String, Array, and Number.

Of course it is not always necessary to write your own code for these extensions to the built in objects since in many cases someone else will have already written such an extension which they will permit you to use. Even where you don't have permission to use methods someone has already written or where you can't find a method that does exactly what you want, viewing the way others have coded their methods will give you a start on how to crerate your own methods to add to the existing JavaScript objects. You are welcome to use any of the many methods that I have created that can be added to these objects to extend their functionality in your page. For example, here are some that I have created to extend the Date object:

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
Related Video
Create 3D Objects with Microsoft Publisher
Creating Webpage Frames

©2013 About.com. All rights reserved.