HTML and CSS Reference
In-Depth Information
There are a number of ways to create objects, but I just describe one for now. To create
an object, you can use an object literal, which is similar to the array literal I just
described:
var car = { color: “blue”, numberOfDoors: 4, interior: “leather” };
That defines an object that has three properties. As long as the properties of the object
follow the rules for variable names, there's no need to put them in quotation marks. The
values require quotation marks if their data type dictates that they do. You can name
properties whatever you like, though, as long as you use quotation marks.
In addition to properties, objects can have methods. Methods are just functions associ-
ated with the object in question. This may seem a bit odd, but methods are properties of
an object that contain a function (as opposed to a string or a number). Here's an exam-
ple:
car.description = function() {
return color + “ car “ + “ with “ + numberOfDoors “ and a “ + interior +
“ interior”;
}
As you can see, this is a bit different from the function declarations you've seen before.
When you declare a method, instead of specifying the function name in the
function
statement, you assign an anonymous function to a property on your object. You can spec-
ify parameters for your methods just as you specify them for functions.
After you've added a method to an object, you can call it in the same way the methods of
built in objects are called. Here's how it works:
document.write(car.description());
NOTE
The core JavaScript language contains lot of built-in objects, too
many to cover here. For more information about these objects,
look at the JavaScript documentation provided by Mozilla or
Microsoft.
I've taken you on a brief tour of the JavaScript language, but beyond the basic language
syntax, which involves declarations, control structures, data types, and even core objects
that are part of the JavaScript language, there's also the browser environment. When your

