1. Technology

Object Oriented JavaScript

3. JavaScript's Built in Objects

By

You do not need to create your own objects in order to use object oriented programming in JavaScript. Everything that JavaScript predefines for you creates a number of objects that your code will interact with regardless of how you write your code. The easiest way to get started in making your code more object oriented is therefore to look at the objects that JavaScript has already defined and to learn how to utilise them in the best way to achieve your desired result.

The one object that you can't avoid referencing in JavaScript that is running in a web browser is the window object since not only is everything that is associated with the browser and the web page associated with that object but also all of the stand alone variables and functions that you define in your code are actually created as properties and methods of the window object. The reason why this particular object is so easy to use in your code without your even realising it is that JavaScript is set up to assume that the properties and methods that your code references belong to the window object if you don't specify which object you are referencing.

That of course brings us to how you actually reference objects, properties, and methods in JavaScript. There are in fact two ways of doing so. The first of these uses a dot after the object name to separate it from the property or method name so that the reference appears as object.property where you want to reference a specific property of a particular object. methods work the same way with object.method providing a reference to the method and object.method() running it. The second way to reference properties and methods of an object uses [] instead of the dot and so we can have object['property'] and object['method']() which mean exactly the same thing. Why have two different ways? Well the dot way is three characters shorter for each reference and is slightly easier to read while the other way is more dynamic since you can substitute a variable for the string in the [] in order to have your code dynamically select which property or method you want to access.

The variables you create in JavaScript can also be treated as objects because JavaScript provides object equivalents to each of its primitive data types and has automatic type conversion so that any attempt to reference a variable as if it were an object will automatically convert it into the appropriate type of object. So any variable containing a number can be treated as a Number object, and variable containing a string of text can be treated as a String object and any variable containing true or false can be treated as a Boolean object.

There are three additional built in object types where there is a short way of defining objects of that type where people using te notation may not even be aware that they are using a built in object. These work in a similar way to strings (where the content is surrounded by " or ') except that the content is surrounded by different characters. Enclosing content inside {} when defining a value to be assigned defines a new object derived from the built in Object object, enclosing the content in [] defines a new object based on the Array object, and enclosing the content in // defines a RegExp object.

JavaScript also supplies a lot of predefined objects that you need to specifically reference in order to use such as Date (which makes date and time manipulations really easy) and Math (which provides access to a number of constant values such as PI as well as a lot of mathematical functions.

The built in JavaScript objects provide plenty of opportunity to start to learn what objects are and how to use them without your needing to create them yourself.

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
Creating Webpage Frames
  1. About.com
  2. Technology
  3. JavaScript
  4. Javascript Tutorials
  5. Object Oriented JavaScript
  6. JavaScript's Built in Objects

©2014 About.com. All rights reserved.