JavaScript Object-Oriented Programming Part 1

An Object

An object is a collection of properties. These properties can either be primitive data types, other objects, or functions (which in this case are called methods, but more on this later). A constructor function (or simply, constructor) is a function used to create an object – this too we’ll discuss in detail later. JavaScript comes with many built-in objects, such as the Array, Image, and Date objects. Many of you are familiar with Image objects from creating those ever-so-cute rollover effects. Well, when you use the code

var Image1 = new Image();  
Image1.src = "myDog.gif";

you have in fact created a new Image object, and assigned a property of your new Image object: the src property. Image1 is a new Image object; in other words, it is an instance of the Image object. Using JavaScript’s dot-structure ( . ), the code above then accesses and sets the src property of your new Image object. Now, let’s learn how to create our own objects.

function myFunc(){  
}  
 
var myObject = new myFunc();  
alert(typeof myObject);  // displays "object"

We’ve just created our own object. In fact we’ve created a myFunc object. myFunc() is a constructor function; it lays out the blueprint from which objects that are created from it will follow (although, in this case, it doesn’t lay out much of a blueprint). So, how does JavaScript know to create an instance of the myFunc object, rather than to return its results? Let’s compare the example above with the following, more conventional use of a function:

function myFunc(){  
 return 5;  
}  
 
var myObject = myFunc();  
alert(typeof myObject); // displays "number"

In this case, we’ve assigned 5 to myObject. So, what’s the difference between these two scripts? Answer: the new keyword. It tells JavaScript to create an object following the blueprint set forth in the myFunc() constructor function. In fact, when we create an Image object, we do the same thing, except that instead of using our own constructor function, we use one of JavaScript’s built-in constructor functions, the Image() constructor function.

So far, we’ve learned how to create a constructor function, and how to create an object from that constructor function. In our example, we’ve created a myFunc() constructor and created an instance of the myFunc object, which we assigned to the variable myObject.

This is all fine and dandy, but what’s the point? Well, just like our Image object, myObject can be assigned properties:

function myFunc(){  
}  
 
var myObject = new myFunc();  
myObject.StringValue = "This is a String";  
alert(myObject.StringValue); // displays "This is a String"

And voila, we’ve now created a property for our object. However, if we create another instance of the myFunc object (using the myFunc() constructor function), we also have to assign the StringValue property to this new instance. For example:

function myFunc(){  
}  
 
var myObject = new myFunc();  
myObject.StringValue = "This is a String";  
var myObject2 = new myFunc();  
alert(myObject2.StringValue); // displays "undefined"

So, how can we create properties that exist for all myFunc objects? Within the myFunc() constructor function, we can do just that. The this keyword inside a constructor function refers to the object that’s being created. Example:

function myFunc(){  
 this.StringValue = "This is a String";  
}  
 
var myObject = new myFunc();  
var myObject2 = new myFunc();  
alert(myObject2.StringValue); // displays "This is a String"

Now, all myFunc objects will have a StringValue property, assigned with the initial value of "This is a String", but every object can have its own distinctive value for StringValue. In other words, we can change the StringValue property for one myFunc object, without affecting the others:

function myFunc(){  
 this.StringValue = "This is a String";  
}  
 
var myObject = new myFunc();  
myObject.StringValue = "This is myObject's string";  
var myObject2 = new myFunc();  
alert(myObject.StringValue); // displays "This is myObject's string"  
alert(myObject2.StringValue); // displays "This is a String"

We can also achieve similar results if we pass arguments to our constructor function:

function myFunc(StringValue){  
 this.StringValue = StringValue;  
}  
 
var myObject = new myFunc("This is myObject's string");  
var myObject2 = new myFunc("This is a String");  
alert(myObject.StringValue); // displays "This is myObject's string"  
alert(myObject2.StringValue); // displays "This is a String"

In the myFunc() constructor, this.StringValue refers to the property being assigned to the newly created object, while StringValue refers to the function’s local variable that was passed as an argument. So, now that we’ve assigned properties to objects, what about methods?

Object Methods

In addition to properties, objects can have methods. An object’s method is a function it can perform. Let’s take a look at this example. For this one, let’s create a Circle object. First, we’re going to have to define our functions, and then make them methods of our Circle object. Let’s define our Circle() constructor and a Circle object or two:

function Circle(radius){  
 this.radius = radius;  
}  
 
var bigCircle = new Circle(100);  
var smallCircle = new Circle(2);

Now, let’s define some functions that we might use:

function getArea(){  
 return (this.radius*this.radius*3.14);  
}  
 
function getCircumference(){  
 var diameter = this.radius*2;  
 var circumference = diameter*3.14;  
 return circumference;  
}

Note that if you were going for accuracy, you could use Math.PI instead of 3.14, but we’ll use this simplified representation of pi to keep the numbers in our examples nice and round.

These functions are easy, except for one thing: what does this.radius refer to? this always refers to the current object, in this case, the Circle object. So this.radius refers to the radius property of the Circle object. So, how do we attach these functions to our object? It’s not as hard as you might think. Let’s change our Circle() constructor:

function Circle(radius){  
 this.radius = radius;  
 this.getArea = getArea;  
 this.getCircumference = getCircumference;  
}

The above assigns the functions getArea and getCircumference to our Circle object, making them methods – functions belonging to our Circle object. We can use methods just like any normal function, but we must first access the object in which the method is encapsulated:

alert(bigCircle.getArea()); // displays 31400  
alert(bigCircle.getCircumference()); // displays 618  
alert(smallCircle.getArea()); // displays 12.56  
alert(smallCircle.getCircumference()); // displays 12.56
Go to page: 1 | 2 | 3 | 4

Win an Annual Membership to Learnable,

SitePoint's Learning Platform

No Reader comments

Comments on this post are closed.