Java Reference
In-Depth Information
is how the objects are created. Objects created from a constructor typically consume less of the
computer's memory than literal objects.
Frankly, it's a question you don't have to worry about at this point in time. It's more important to
know how to create objects than using the correct approach. So practice both methods; create your
own custom objects and reference types!
summarY
In this chapter you've taken a look at the concept of objects and seen how vital they are to an
understanding of JavaScript, which represents virtually everything with objects. You also looked
at some of the various native reference types that the JavaScript language provides to add to its
functionality.
You saw that:
JavaScript is object‐based—it represents things, such as strings, dates, and arrays, using the
concept of objects.
Objects have properties and methods. For example, an Array object has the length property
and the sort() method.
To create a new object, you simply write new ObjectType() . You can choose to initialize an
object when you create it.
To set an object's property value or get that value, you simply write objectName
.objectProperty .
Calling the methods of an object is similar to calling functions. Parameters may be passed,
and return values may be passed back. Accessing the methods of an object is identical to
accessing a property, except that you must remember to add parentheses at the end, even
when it has no parameters. For example, you would write objectName.objectMethod() .
The String type provides lots of handy functionality for text and gives you ways of finding
out how long the text is, searching for text inside the string, and selecting parts of the text.
The Math type is created automatically and provides a number of mathematical proper-
ties and methods. For example, to obtain a random number between 0 and 1, you use the
method Math.random() .
The Array type provides ways of manipulating arrays. Some of the things you can do are find
the length of an array, sort its elements, and join two arrays together.
The Date type provides a way of storing, calculating with, and later accessing dates and times.
JavaScript lets you create your own custom objects, giving them the properties and methods
that you want them to have.
JavaScript enables you to create your own types of objects using reference types. These can
be used to model real‐world situations and for making code easier to create and more main-
tainable, though they do require extra effort at the start.
 
Search WWH ::




Custom Search