|
||||
|
||||
|
3 ~ Objects and Hierarchy(1) (2) (3) (4) (5) (6) (7) (8)One of the fundamental things you will have to understand about programming in Javascript is hierarchy. In a nutshell, this simply means the order in which your code is written. Objects are just things, and you can think of objects as nouns. A noun is a person, place or thing, and an object in "javascript" terminology can be thought of in the same way. The word 'document' is an object, and 'Form' can be an object also; and here is where things can get confusing. 'Form' can also be a property of the document when used after the word 'document' in a script. It makes a little more sense if you think of it like this: both a car and its bumber are objects, but the bumber belongs to the car and is therefore also a property of the car. Can you see where hierarchy can fit into this picture of the car and bumper? Uppermost, or first, would be the car; next down the line is the car's bumper, and maybe a little lower than the bumper would be the chrome on the bumper. All three things are objects, but the last two are also properties. The car is also a property, when referenced by its owner. In "javascript" the code you write also has hierarchy. Take a look at the following: document.myForm.myText 'Form' in this instance is a property of the object 'document'. When reading code you can tell this is so simply because it comes after the word 'document' in the script. Take a peek at the picture below to help you get a better idea of all this. ![]() Sticking with our topic of hierarchy, we'll start at the top of the diagram. Uppermost is Windows (not the OS, the browser window); the words 'frames', 'parent', 'self', and 'top' are types of 'window' and on the same level. These five terms need not be used in a script unless there is more than one of each-- i.e. a framed website (two or more windows). For instance you need not always write "window.document.write" because the window is assumed to be there anyway; a simple "document.write" is fine. Next down the line are 'location' and 'document' and these are on the same level. These are both properties of 'window' and are also objects in and of themselves, and can have their own properties. 'IMG' is an object and is also a property of 'document'. I realize that this can be somewhat confusing but it may help if you understand that the window could not be a property of a checkbox; and that a document is used for display in a window, a form tag is written in a document, a checkbox is in the form and so on. If something is a part of or inside of something bigger or more dominant, then it's hierachy is lower than whatever it is in. There's logic to all of this, once you get the hang of it. Why is all this so important? Well, because the part of the browser that handles "javascript" is designed to handle things in a certain form and order, and your code just won't work if this isn't adhered to. You can write a script in this order: document.form.checkbox...but not in this order: form.document.checkboxWe've only dealt with a very few objects and their properties, but you should be able to get the idea from the above info and with a little practice. I know I've repeated myself a couple of times, but the idea of hierarchy has to be understood if you want to learn "javascript". When you examine someone else's code, knowing hierarchy will often help you figure out exactly what is going on, and looking at other programmers' scripts is one of the tools you should use in order to learn. Don't be afraid to use some of the routines you run across in your own scripts; There's nothing wrong with that, unless you copy a whole script and try to pass it off as your own. I have seen, however, countless little run-of-the-mill "javascript"s on some of the cut-and-paste sites that come complete with copyright declarations and other silly statements that are for the most part a meaningless attempt to impress somebody. JavaScript is a fairly structured language, and if you write anything at all, you will use the same routines that others have used by necessity. Zane Grey and Robert Heinlein both wrote many novels, each sticking to the rules of English, and each using identical phrases from time to time, with neither one plagiarizing the other's work. To sum up my little speech, do you think this can be copyrighted? document.write("Hello, World!")...believe it or not, I ran across this exact script at a download site that was claiming copyright. (Try to write it any other way.) Lesson 4 (1) (2) (3) (4) (5) (6) (7) (8) |