Java Reference
In-Depth Information
Creating neW tYpes of objeCts (referenCe tYpes)
This section's focus is on some advanced stuff. It's not essential stuff, so you may want to move on
and come back to it later.
You've seen that JavaScript provides a number of objects built into the language and ready for
us to use. You've also built custom objects that you can use to represent more complex data,
but JavaScript also enables you to create your own type of objects. For example, you created an
object that represented an individual person, but you can also create an object that is a Person
object.
It's a bit like a house that's built already and you can just move on in. However, what if you want to
create your own house, to design it for your own specific needs? In that case you'll use an architect
to create technical drawings and plans that provide the template for the new house—the builders use
the plans to tell them how to create the house.
So what does any of this have to do with JavaScript and objects? Well, JavaScript enables you to
be an architect and create the templates for your own objects to your own specification, to fit your
specific needs. Going back to the person object example, JavaScript doesn't come with built‐in
person objects, so you'd have to design your own.
Just as a builder of a house needs an architect's plans to know what to build and how it should
be laid out, you need to provide blueprints telling JavaScript how your object should look.
You somewhat did this with the createPerson() function in ch5 _ example8.html , but you
only created plain objects with custom properties and methods—you didn't create an actual
Person object.
But JavaScript supports the definition of reference types . Reference types are essentially templates
for an object, as the architect's drawings are the template used to build a house. Before you can use
your new object type, you need to define it along with its methods and properties. The important
distinction is that when you define your reference type, no object based on that type is created. It's
only when you create an instance of your reference type using the new keyword that an object of
that type, based on your blueprint or prototype, is created.
Before you start, an important distinction must be made. Many developers refer to reference types
as classes and use the two terms interchangeably. Although this is correct for many object‐oriented
languages such as Java, C#, and C++, it is not correct for JavaScript. JavaScript does not yet support
a class construct, although the next version of JavaScript will provide formal classes. JavaScript
does, however, fully support the logical equivalent, reference types.
It's also important to point out that the built‐in objects discussed thus far in this chapter are also
reference types. String , Array , Number , Date , and even Object are all reference types, and the
objects you created are instances of these types.
A reference type consists of three things:
A constructor
Method definitions
Properties
 
Search WWH ::




Custom Search