Objects in JavaScript

By David Wall, ITworld |  How-to

JavaScript has a much more limited knowledge of the world than you and
I. The objects it knows are all figments of computer memory, but those
figments are your concern when trying to design functional, interactive
Web pages.

JavaScript identifies its objects the same way you do, via a hierarchy
of progressively more specific objects. JavaScript knows that a browser
window, for example, has certain characteristics, such as size and
resize capabilities. The language knows how to make browser windows do
certain things, such as close. JavaScript knows the characteristics
(which it calls properties) and capabilities (which it calls methods)
of a whole bunch of objects that have to do with browsers and the
presentation of HTML documents. Collectively, these objects make up the
Document Object Model (DOM).

The syntax for referring to properties and methods of objects may seem
a little bit unusual at first. JavaScript identifies objects by a
series of keywords separated by periods. Here's an example:


Don't panic. That hierarchy refers to a text field contained in a form,
which is a part of the document in frame 1 of the browser window. See?
Not too hard.

Once you've used the correct JavaScript syntax to identify the object
you want to work with, you can go on to specify the property you want
to inspect or the method you want to employ. Let's say you want to
refer to the value property of that form element, which contains the
contents of the text field in this case. In JavaScript, you'd say this:


The property just gets tacked onto the end of the object specification.

When you refer to methods, you have to put parentheses after the
method's name. The two parentheses are there to hold parameters that
the method needs in order to do its stuff. You can sometimes leave them
empty. In other cases, they should hold numbers, strings of characters
or the URL of a needed resource that resides somewhere on the Internet.

Fun fact: There's a difference between an argument and a parameter,
even though the words frequently are used as if they were
interchangeable. Parameters are the variable -- like definitions that
appear in function declarations. Arguments are the values that are
actually sent to the function when it is called. I have to go now; Star
Trek is on.

Join us:






Answers - Powered by ITworld

ITworld Answers helps you solve problems and share expertise. Ask a question or take a crack at answering the new questions below.

Ask a Question