Today we are going to talk about objects and what makes up an object. Quite frankly, Objects have always been confusing to me. So don't feel bad if you have a problem understanding the concepts at first. Just bare with me and hopefully you will get a good grasp of how to handle objects by the end of this course.
We will also be talking about the dot syntax which is used when we want to refer to specific properties and methods of an object. We will then discuss the String object, one of 4 stand alone objects we will talk about in this class. Finally, we will put all of this together to develop a script to scroll text on your page.
Some text books and instructors like to compare a programming object to a real world example (like a cat, tree and etc.). This is fine except all of these objects are something that you can actually put your hands on and the objects we are going to talk about here are not. Therefore, I am going to take a little different approach to describing objects. Actually, I think what you read here will be a more realistic approach.
Ok get a cup of coffee or a soda and settle back in your chair and here we go!
Everything you see on your screen is there because someone wrote a program for it. All of the windows you see are generated by a program. When you move a window it appears that you are moving an object on the screen when what you are really doing is running a quite elaborate program that keeps redrawing your window hopefully fast enough that you don't notice it.
Since all windows are basically the same, the programming is written for it in what is called object oriented programming. By designing windows by a basic design specification any window can be created and controlled by using only one program rather than having to write a separate program for each window. When you open up your mail program, it is inside a window. Now open up your browser. It too is contained inside a window. You can change certain aspects of these windows. You can make them narrow or wider for instance. But remember there are only certain things you can change. All of the windows work basically the same.
Programmers decided to give these things that they are controlling a name. The name they choose is object. So each window on your screen is an object. If you change the size of that window, then you are changing one of the properties of that window. So the window has a width and that is a property. It also has a height that is a property. You can change both of these properties. The window has properties that you can't change. You can't change the basic outline color of the window for instance, nor can you change the basic rectangular shape.
It is very important that you understand what has been said so far. Programmers decided that they wanted to have a common program to generate like items on a computer. These items are called objects. A window is an object that has been created with a program. The programmers decided that they were going to allow us to change certain things about an object. These things that we can change are called properties. Now from a programming standpoint what do you think these things that are called properties really are. They are variables.
So, now we have a program that has variables. What other thing would you guess this program might have. How about functions. Yep, and they had to call them methods just to confuse us. So, all an object really is, is a program with variables and functions (ie Object with properties and methods). It also has many variables and functions that operate behind the scenes that we can't get to. We can only access certain properties of an object and use certain methods.
Objects can contain other objects. A window contains documents with the document being your actual web page. Documents contain images, forms, tables, links, and etc. All of these items are objects of the document. The object contained by another object is a property of that object. Remember that we learned that variables, i.e. properties in this case, can have a value of the object type.
So, when you think about it, objects are really quite simple. They are just programs that someone has written for us that have properties and methods. Some of the properties we can change and others we can only get their value. We also realize that these objects have properties and methods that are internal and not available to us.
You will referring to the properties and methods of an object quite
its properties and methods. The one you have seen the most often is
Here you are using the method write() of the document object. As we
discussed earlier, an object can contain properties that are actually
other objects. One such property is the form. Remember that I said that
giving the form a name is important. Well, that's how we refer to it. The
form contains other objects that are properties of it. An example would be
a text input box. The text input box has an important property, value.
This property is the text in the box. It is both readable and write able.
You can get this value by using the following syntax:
Lets look a simple example to see how we use the dot syntax to refer to properties. Type anything into the lower text box marked Input. When you press the first button the text you typed appears in an alert box. Pressing the second button displays your text in the output text box.
Notice that we have a form named "demo" that contains the two text input boxes and the two buttons. Look at the onClick event for the first button. It contains an alert box call with the parameter document.demo.inP.value. This is how we read the value property of the text box object named "inP" that is a property of the form object named "demo" that is a property of the document object (I had to use short names here so that they would fit on this web page properly. Longer names might be more descriptive.). So I hope this makes it clear as to how to refer to an object. You start at the top most object "the document" and follow each property until you get to the property you are interested in.
Look at the onClick event on the second button. It uses the dot syntax to set the value of the top text input box equal to the value of lower input box when the button is clicked. So that's all that is needed to copy the text from the bottom box to the top.
Spend some time and try to understand how to reference objects using the dot syntax. You will find that you spend a lot of time getting the reference to an object correct.
We will now begin our discussion of stand alone objects, objects that are not part of another object, with the String object. The Date and Array objects will be also be discussed in detail during the upcoming lessons. There are a few other standalone objects but these 3 are the ones you will use most often.
You will use the new operator to create each of the stand alone objects we will discuss in this class. The syntax to create a String object is as follows:
The String object contains quite a few properties and methods. We will only cover one of its properties and one of its methods in this class. You can learn about the rest in one of the reference books in our Library.
Note that there is almost no difference between a String Object and a string value. Both allow you to use the same methods and the most frequently used property length.
The substring() method has a syntax
Try each one of these examples in our lab
I do not recommend that you put your banner in the status bar because it will prevent the useful information that is normally displayed there from being seen. A better approach is to put your banner in a text box of a form. Look at the following demo.
Take a close look at what is happening in the above demo. Notice that each time you press the "Test" button the first letter of the string in the text box is being removed and put at the end of the string. This makes the text appear to scroll. Note that the text box would normally be shorter than the length of the string in it. I made it longer here so you could see what is going on.
Here is the function that I used for the demo.
The script contains a global variable msg. It is written to the text box every time the function is called. The last line of the function reconstructs the string msg for the next time it is written to the text box. Notice that the substring() method is used twice, the first time it returns all of the original message except for the first character. The second time it returns only the first character of the string. This results in a new string with the first character moved to the end of the string.
The function is called using the onClick event of the 'Test' button. Some other method is required if you want the script to work automatically. The setTimeout() method of the window object is used for this purpose. It is normally the last line in a function that is being used to scroll text. This method is used to call a function after a certain time has elapsed.
The setTimeout() method that we would use in our script above would probably look something like this.
So in our example, the message would continue to scroll after the function is initially called because the script would be called again by the setTimeout() method after a delay of 200 milliseconds. Thus we have a continuous loop.
There is still one thing missing. We need some way to get the scrolling message started. In other words we need some action that will call our function the first time. The event handler onLoad of the BODY tag is normally used for this. This event occurs when everything in the document has completed loading. So all we have to do is call our function from the onLoad event handler of the BODY tag similar to the way we called the alert box from the Button in the previous lesson.
[ Top of Page | Contents ]