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.
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, [email protected]” and “Mustafa Qureshi, 65, [email protected]” are two instances of the Person object.
Having said all this, we can creaet several game characters like this:
aCharacter = new GameCharacter ("Hiloo Bhai", 70, "Free Style");
anotherCharacter = new GameCharacter ("Farhat Saleem", 65, "Martial Arts");
If you now take a look at the constructor defined in the last section, you will understand the way parameters assign starting values to the object.
Something About “this”
Coming to the this part. Take a look at the GameCharacter constructor once again. The values passed to the constructor are received in variables myName, myStrength and myStyle. Thus for new GameCharacter (“Hiloo Bhai”, 65, “Free Style”) the values of these variables are “Hiloo Bhai”, “65” and “Free Style” respectively. But these my… variables are temporary or local. They will not retain their values once the function exits. To make these values as the permanent properties of aCharacter, we have used the this pointer.
Joining The Pieces
Lets try a simple working example that puts all the pieces together. The following code defines an object GameCharacter; creates two instances of it and writes them to the page with document.write().
The following important concepts were covered in this article:
- The definition of an object is the specification of its properties and methods.
- The creation of an object means making a new instance of an already defined object (also called class). This instance will have its own unique identy.
- We create instances of objects with the new statement.
- A constructor is a function that has the same name as that of the object. Its automatically called when new statement is executed.
- A constructor usually assings values to the properties of the object.
Note: The names and email addresses used in this document are completely hypothetical and any resemblence is purely coincidental.