JavaScript has object-oriented capabilities which make it useful for interacting with the DOM, which is a kind of map that holds information about all the objects on a web page.
In the context of a web page, a JavaScript object is any HTML element within a document that may be accessed using JavaScript.
Objects and Properties
Objects have properties and in some cases these properties are themselves objects. A property can be considered an object if it has it's own properties.
For example, in an HTML document that contains an image, the document is an object that has an image property. The image is itself an object that has a width property.
Objects and properties are a hierarchy and are accessed by separating each object and property with the . character as shown below.
In the code above the first object in the hierarchy is document. Image is a property of document but is itself an object that has a property width.
Properties are a bit like variables that may contain any type of data including arrays. And in the case above the image was accessed using an index just like an array.
Since the image was the first image object within the document it was accessed as index 0.
Conveniently, objects may be accessed by their HTML names - making for much more readable code, as shown below.
A function may be a property of an object and as such is referred to as a method. It is distinguished from other properties by the parenthesis following it that normally contains arguments.
In the examples above, write is a method of the document object that takes a variable or a literal as an argument.
Below is another example of a method that uses an onload event handler (events are covered in the Running a Script Tutorial).
The focus() method, appended after the hierarchy of objects, is used to place the cursor in the text field of a form.