The Cellar

The Cellar (http://cellar.org/index.php)
-   Technology (http://cellar.org/forumdisplay.php?f=7)
-   -   Learn Javascript (http://cellar.org/showthread.php?t=5389)

hot_pastrami 03-23-2004 04:25 PM

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.

lumberjim 03-23-2004 10:43 PM

I'm right with you. I have to read each line of your description 3 times, but it makes sense if I read slowly. I have a question, though. The very first characters of each code....are those commas, or periods?



it seems like they don;t matter. I did this without, and it works:

this will be funnier for you if you don;t read the code first, but just paste it into the tester and run it.......but it still won;t be all that funny, so don;t get all excited



var prompt = "Click OK if you understand this shit.\nClick Cancel if it's making your hair hurt.";

if (confirm(prompt)) {
document.write("yeah, bullshit, fonzie");
}
else {
alert("You're pretty dumb.");
document.write("yup, pretty dumb");
}

hot_pastrami 03-23-2004 11:09 PM

Quote:

Originally posted by lumberjim
The very first characters of each code....are those commas, or periods?
I'm not sure what you're referring to here...

lumberjim 03-24-2004 05:37 AM

Quote:

Originally posted by hot_pastrami

I'm not sure what you're referring to here...

code:
.
var make = "Chevrolet";
var model = "Camaro SS";
var exteriorColor = "black";
var interiorColor = "charcoal";
var horsePower = "410";
.


are these even part of the code, or are you just putting them there out of habit?

Happy Monkey 03-24-2004 06:33 AM

What browser are you using? On mine, those are horizontal lines - not pat of the code at all.

jinx 03-24-2004 06:47 AM

-lumberjim

netscape 7.1

jinx 03-24-2004 06:51 AM

1 Attachment(s)
see?

hot_pastrami 03-24-2004 10:17 AM

Quote:

Originally posted by lumberjim
are these even part of the code, or are you just putting them there out of habit?
Those are supposed to be horizonatal lines, automatically added when i use the "code" vB tags. Hmm. Netscape must have issues with horizontal lines under some circustances.

What are you doing running Nutscrape anyway? What kind of daft, running-around-with-limp-wrists silly person would choose Netscape over Mozilla? :D

lumberjim 03-24-2004 10:40 AM

I've been through this with syc and UT. I LIKE netscape. Mozilla appears to just be askin to my netscape anyway, and it does not load IM, or my tabs. I have to tell it to show me navigator tabs, and it moves slower. I'll just ignore the dots.

browser snob

Radar 03-24-2004 10:46 AM

Less chatting, more programming please.

hot_pastrami 03-24-2004 10:58 AM

Quote:

Originally posted by lumberjim
browser snob
Man, we're just trying to help... the first step to recovery is admitting that you have a problem. So, let me take care of that right now... I admit that you have a problem. Ok, now we can move on to the second step... bitching at you about your uber-lame browser, with really smarmy quips like this:

"Netscape is the short bus of web browsers!"

"I'm not saying that anybody who uses Netscape is a dumb person, I'm just saying that even smart people are capable of doing dumb things."

"Allow me to disabuse you of the notion that your browser of choice is in any way superior to a rotting heap of rhino feces!"

I forget what the third step is, something about "intervention" or "hypertension" or "Star Trek convention." But we'll deal with that later.

Programming... right. More to come soon.

hot_pastrami 03-24-2004 03:18 PM

Well, things at work just got crazy, so there problably won't be anything new today. If I have time tonight I'll post something, though.

lumberjim 03-24-2004 06:06 PM

BUTTONS
 
hey, this is probably premature, but, how do you change the words on the buttons themselves?

does JS support that, or must you create a whole new code or object to be that prompt?

oh, and can you crack the code in a browser or OS and make it change the word that displays on the normal buttons? ie. in stead of the button saying "OK" on it, it could say " Sure, why not"

lumberjim 03-24-2004 06:37 PM

{(document.bgColor = "red");
(document.write("Coolness Quotient Test Site"));
alert ("click OK to begin test");
document.write(" Results: You're a tool");
}

how do you change the colors of the words?
how do you clear the page to display the results?
how do you change the location of the text on the page?

hot_pastrami 03-25-2004 11:46 PM

Sorry about the delay, I'm currently being taken advantage of by my employer. I like working seven days a week for 12 hours per day.

I'll post more as soon as I am able.


All times are GMT -5. The time now is 12:35 AM.

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