Freelance Writing Jobs | Today's Articles | Sign In

 
Browse Sections

Creating Custom Objects



Skill Level: Intermediate
Skills Required: HTML and JavaScript Functions and Built-in Objects



Abstract:

You may be knowing that JavaScript is an object based language, but not object oriented. What does this mean? If you know any object oriented programming language - Java, C++ or Smalltalk, for example - then you might be knowing that inheritance is an important concept in object oriented programming. This feature is missing in JavaScript. That's why its said to be object based and not object oriented. Even if you don't know object oriented programming, you have surely used objects like document, window and form in JavaScript.

Does this mean that you can create your own objects in JavaScript? Absolutely right! JavaScript gives you the power to define your own custom objects to handle complex tasks. In this article, we will explore the basics of object creation, constructors and syntax for defining properties. Advanced topics like defining methods of an object and modifying existing objects will be covered in some later article.


Defining Your Own Objects:

To define your objects, you need a constructor. The concept is similar to other object oriented languages and the syntax is similar to C++ and Java. The following code segment creates an object named GameCharacter to store information regarding a hypothetical game called Galee Ke Padhay Baz. Please bear with me - the name is in my native language, Urdu.

Lets suppose we want to have properties that hold values for the game characters' name, strength and fighting style. The constructor is as follows:

function GameCharacter (myName, myStrength, myStyle)
{
   this.name = myName;
   this.strength = myStrength;
   this.style = myStyle;
}

I can hear you mumber: "I can understand most of it, but what is this this thing?" Don't worry, I am explaining it. But first some other important things.


Constructors Explained:

First of all, note that the name of the function is the same as the name of the object we want to define. This is always the case for a constructor. If you want to define an object called Card then your constructor should be defined like function Card (...).

Next, observe that the constructor is being passed three parameters. These parameters will be containing the starting values when an object is created. There is a major difference between the terms: created and defined. By definition, I mean the structure / methods of the class of objects. And by creation I mean a specific object. An example will help here. Consider the class of Person. Its definition will include things like his / her name, age, email, etc. Every instance of Person will share this structure. But the information contained in these properties will be different for different instances of Person. Once an object is defined we can create several instances of it. For example, "Tom Cruise, 30, tom@aol.com" and "Mustafa Qureshi, 65, qureshi@cyber.net.pk" are two instances of the Person object.

The copyright of the article Creating Custom Objects in JavaScript is owned by Muhammad Ali Shah. Permission to republish Creating Custom Objects in print or online must be granted by the author in writing.

Go To Page: 1 2

Articles in this Topic    Discussions in this Topic