The Cellar  

Go Back   The Cellar > Main > Technology
FAQ Community Calendar Today's Posts Search

Technology Computing, programming, science, electronics, telecommunications, etc.

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
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
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT -5. The time now is 01:03 AM.


Powered by: vBulletin Version 3.8.1
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.