1. Computing

Object Oriented JavaScript

JavaScript is a prototyping language where unlike most object oriented languages you don't use classes but instead create objects from other objects. In these tutorials we will look at how to write object oriented code the JavaScript way.

Object Oriented JavaScript
JavaScript is a prototyping language rather than an object oriented one but most of what you can do with an object oriented language can be done with JavaScript.

What is Object Oriented JavaScript?
Introducing a series of tutorials on how and why to use an object oriented approach to writing JavaScript.

The Benefits of OO JavaScript
Why should use use an object oriented approach to JavaScript? Here are some reasons.

JavaScript's Built in Objects
You don't have to create all your own objects in JavaScript. JavaScript provides a number of objects built into the language to get you started on object oriented processing.

Extending Built In Objects
It isn't necessary to define your own objects completely from scratch in JavaScript. Simply take an existing object that handles part of what you want and add methods to it to do the rest.

Creating Objects from Existing Objects
The easiest way to create a new object in JavaScript is to copy an existing object.

Creating New Objects Without Copying Existing Ones
You don't have to directly copy another object in JavaScript when creating a new one. There are more sublte ways of creating new objects.

Dot Notation and "this"
The dot notation allows us to identify which properties and methods apply to which objects while this allows us to reference the current object without knowing its name.

prototype
Using the prototype property of objects avoids the duplication of code for mathods attached to similar objects.

Inheritance and "constructor"
Objects can inherit from other objects in JavaScript. The mechanism is a little different from other languages.

Associative Array Notation
JavaScript Objects can be treated as associative arrays which allows you to use variables to contain property and/or method names.

Create Method if Doesn't Exist
Some really useful methods have been added to some objects in some but not all browsers. Use the built in method where it exists and add your own when it doesn't.

"self" and Multiple Objects
Once you start attaching event processing to objects different browsers associate 'this' with different objects. We can make sure we know what object we are referencing by copying 'this' into our own property - usually called 'self'.

Defining Objects with JSON
JavaScript Object Notation is a short way of defining objects. It is so convenient that it is commonly used with Ajax for passing objects from the server.

Namespaces
In JavaScript we can use objects as if they were namespaces so as to avoid naming clashes.

Lazy Definition
We can improve the efficiency of our JavaScript by dynamically choosing which version of processing the particular browser needs and making that choice only once the first time the code is used.

Extending Methods
When you define a new object in JavaScript you may want to extend the functionality in some of the methods that the parent object has.

Copying Objects
There is more than one way of copying objects in JavaScript depending on just exactly how you intend using the copy.

Private Properties and Privileged Methods
The difference between how you define private and puvlic properties in JavaScript is not as obvious as it is in other languages.

Public Access to Private Methods
One potential problem with public methods is that they may be overridden before you call the constructor which can break the constructor if the constructor needs to call the original version.

Chaining Methods
If a method doesn't have a useful value to return then returning a pointer to the current object will make it possible to chain methods together.

Singletons
All JavaScript objects are singletons as JavaScript doesn't have classes. That doesn't stop you accidentally creating separate singletons though where you intended only one, unless you add a little code.

©2014 About.com. All rights reserved.