![]() |
|
Technology Computing, programming, science, electronics, telecommunications, etc. |
|
Thread Tools | Display Modes |
![]() |
#11 |
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"; 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?"); Code:
document.write("Some text"); Code:
alert(window.location); More later.
__________________
Hot Pastrami! |
![]() |
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|