Creating an Object
We have already seen an example of an Array object being created. To create an Array object, we used the JavaScript statement
var myArray = new Array();
So how is this statement made up?
The first half of the statement is familiar to us. We use the
var keyword to define a variable called myArray. This variable is initialized, using the assignment
operator (=), to the right-hand side of the
statement.
The right-hand side of the statement consists of two parts. First
we have the keyword new. This tells JavaScript that we
want to create a new object. Next we have Array(). This
is the constructor for an Array
object. It tells JavaScript what type of object we want to create. Most objects
have constructors like this. For example, the Date
object has the Date() constructor.
we can pass parameters to the constructor Array() to
add data to our object. For example, to create an Array
object that has three elements containing the data "Paul", "Paula", and "Pauline", we use
var myArray = new Array("Paul", "Paula", "Pauline");
Let's see some more examples, this time using the Date object. The simplest way of creating a Date object is
var myDate = new Date();
This will create a Date object containing
the date and time that it was created. However,
var myDate = new Date("1 Jan 2000");
will create a Date object containing the
date 1 January 2000.
How object data is stored in variables differs from how primitive
data, such as text and numbers, is stored. (Primitive data is the most basic
data possible in JavaScript.) With primitive data, the variable holds the data's
actual value. For example
var myNumber = 23;
means that the variable myNumber will
hold the data 23. However, variables assigned to objects don't hold the actual
data, but rather a reference to the memory address where
the data can be found. This doesn't mean we can get hold of the memory
address—this is something only JavaScript has details of and keeps to itself in
the background. All you need to remember is that when we say that a variable
references an object, this is what we mean. We show this in the following
example:
var myArrayRef = new Array(0, 1, 2); var mySecondArrayRef = myArrayRef; myArrayRef[0] = 100; alert(mySecondArrayRef[0]);
First we set variable myArrayRef
reference to the new array object, and then we set mySecondArrayRef to the same reference—for example, now mySecondArrayRef is set to reference the same array object.
So when we set the first element of the array to 100 as shown here:
myArrayRef [0] = 100;
and display the contents of the first element of the array
referenced in mySecondArrayRef as follows:
alert(mySecondArrayRef[0])
we'll see it also magically has changed to 100! However, as we now
know, it's not magic; it's because both variables referenced the same array
object because when it comes to objects, it's a reference to the object and not
the object stored in a variable. When we did the assignment, it didn't make a
copy of the array object, it simply copied the reference. Contrast that with the
following:
var myVariable = "ABC"; var mySecondVariable = myVariable; myVariable = "DEF"; alert(mySecondVariable);
In this case we're dealing with a string, which is primitive data
type, as are numbers. This time it's the actual data that's stored in the
variable, so when we do this:
var mySecondVariable = myVariable;
mySecondVariable gets its own separate
copy of the data in myVariable. So the alert at the end
will still show mySecondVariable as holding "ABC."
To summarize this section, we create a JavaScript object using the
following basic syntax:
var myVariable = new ObjectName(optional parameters);