View Single Post
Old 03-23-2004, 04:25 PM   #11
hot_pastrami
I am meaty
 
Join Date: Dec 2001
Location: Salt Lake City, UT
Posts: 1,119
So now you should have an idea what a variable is, and what a function is. A variable is a piece of data, and a function is code that you can pass arguments to in order to execute one or more actions. So that leads us to:

OBJECTS

Let's say you wanted to write a program that simulates a car. You might define a few variables to set the parameters of the car, like so:
Code:
var make = "Chevrolet";
var model = "Camaro SS";
var exteriorColor = "black";
var interiorColor = "charcoal";
var horsePower = "410";
...and you might create functions for your car, like start(), accelerate(), brake(), etc (I'll get into how to create your own functions shortly).

Well, that's all fine and dandy, but what if you wanted your program to simulate two cars? Do you add the car name to the variables, and define an entirely separate set for the new car? Perhaps. But what if you want to simulate 100 cars? That would be a lot of variable naming. This is one useful application of programming Objects.

An object is a variable which has it's own sub-set of variables and functions. I can define an object called Car, which contains all of the above variables and methods. Then, every instance of Car I create has it's own unique values for those variables, and when I run a function for one of the Car objects, the other Car objects are unaffected.

A bit later we'll get into how to create custom objects, but Javascript does automatically create a bunch of objects automatically, such as the document object, which is created to allow you access to the webpage you're working with.

To access an object's variables and functions, you provide the object variable, and then a period, and then the variable name of function name that you want. For example, with our fictional Car object:
Code:
camaroSS.accelerate();
mustangGT.accelerate();
alert("Who's your daddy?");
...or, for something that uses a built-in object:
Code:
document.write("Some text");
...this will print the words Some text to the browser window. Javascript also creates an object called window to provide access to the browser window. You can use the alert() function we went over previously to display the contents of the window object's location variable, which contains the current URL:
Code:
alert(window.location);
Don't worry if you don't understand it fully, objects can sort of be learn-as-you-go.

More later.
__________________
Hot Pastrami!
hot_pastrami is offline   Reply With Quote