document.write("Hello World") and other cases of something-dot-somethingelse

When you look at a JavaScript program, you usually see a bunch of things of the form word dot word parenthesis (maybe with something in the parenthesis or more word, dot pairs.) For instance, in the program in the article A First JavaScript Program (available here), we have several such lines. I'm going to look at document.write("Hello World!"), because it makes a particularly good example. So, what is this and what does it mean? These are examples of objects and methods. Although understanding objects and methods is important if you want to really become a good programmer in object-based (ie JavaScript) and object-oriented (ie Java) languages, knowing how to use them allows you to start reusing other people's code. I also find that it is easier to grasp the theory behind objects and methods after playing with them a little bit. This article should help you gain enough understanding to recognize objects and methods and use a few simple methods.

The line document.write("Hello world!") is a good example to learn from because it illustrates the basic concepts in a very basic way and it is very useful for simple programs. Objects are virtual items in your program. In our example, we are dealing with the document object. In a web page, this is the actual contents of the page (and technically, this is part of the W3C Document Object Model, DOM, not specifically JavaScript, but JavaScript interacts with the DOM.) Keep in mind for the future that not all objects tie in to visible things so clearly, but it's always nice to start with the easy part. Methods belong to specific objects and are called as object.method_name(properties and values). In this case we are looking at the write() method of the document - document.write().

Methods can accept properties and values that tell the method more specifically what to do, these go in the parenthesis. For instance, you might tell a child to sit down or you might want to tell them to sit down right now in the blue chair. The "right now" and "blue chair" part would be properties and values. Just like a child unfamiliar with the word might just look at you confused if you told them to sit down on the chaise, you can only tell an object or method about activities and things they are familiar with (unless you teach them about them first.) In our case, we are passing the value "Hello world!".

The document.write() method assumes anything you pass it is text, HTML, or JavaScript that it should print to the web page. So our example line document.write("Hello world!") prints Hello world! on the page. If you want to write anything on a web page using JavaScript, you can do it simply by putting it in your program between the parenthesis of a document.write() method call. You can include one or more arguments to document.write() and they will just be displayed in the order given. Different objects have different methods and different methods use the information they are given differently (and sometimes objects have sub-objects), but the same basic pattern of object.method() applies.


document.write( argument_one[, argument_two, ...])

writes the given arguments, which can be any combination of text, HTML, and JavaScript , to the document (web page).

The W3C Document Object Model (DOM)

This site needs an editor - click to learn more!

You Should Also Read:
A First JavaScript Program
Beginners Resources
JavaScript/Java Newsletter

Related Articles
Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map

Content copyright © 2023 by Julie L Baumler. All rights reserved.
This content was written by Julie L Baumler. If you wish to use this content in any manner, you need written permission. Contact BellaOnline Administration for details.