Gw Temp

Menu

Article - 'Objects in JavaScript' by Guest

An item about Miscellanious posted on Aug 8, 2003

Blurb

Another damn good Sphere tut by Blue Moogle, this one on objects.

Body

======================
Objects in Javascript
by blue_moogle
======================

CONTENTS:

1. The Introduction
2. What are OBJECTS
3. Help me help you help them!
4. The Review
5. Conclusion




---------------------
1. The Introduction
---------------------

You can't really use Javascript until you've used Objects. Objects for some reason makes a scripting language "THE BEST" that and fame (like C++). VB is not object oriented, giving it a disadvantage. This is also why its easier to learn.. cause you don't have to learn objects (pwah). Objects esantially are like... evolved variables. Take your functions and your variables and breed them to get objects (how fun!). So lets learn these mighty so hard to learn objects for your scripting life!



--------------------
2. What are OBJECTS
--------------------

Its hard to explain objects really. They are basically variables with the power of a function. Like a variable that can store function powers. Neat it is. Why that would ever be useful is unknown to you at the moment, but it has its powers. Plus its a load easier to read. Trust me. Lets just take a lookie at what a variable, a function, and an object looks like, and you choose which you like the best.

1. var bill_the_toe = "42 is the meaning of life"

2. grandpa_jelly(shoesize, funky message, power-rangers)

3. player1.hp = 9999

If you have ANY experience, you can easily find which is the object. Just look for the purty one. OK, its the player1.hp thinger bobber. OH, now you get it... shake your head yes dommit. Fine, you don't give a damn that it looks pretty, do you? You want a big fat reason why you should learn objects. Ok, I don't know besides the fact they can have function powers and stuff.. aka HAVING SCRIPTS FOR VARIABLES. Yea, thats it. Like heres something:

player.change = 9999

When making that little bit, you could of had MILLIONS of lines of code behind that. And it would check all the time to see if it would ever become 9999, and if it did, it would change it maybe. SEE, a variable that does stuff on its own is something.. I guess... I myself have to learn to do that >_< So we'll stick to basic function/variable combos.




-------------------------------
3. Help me help you help them!
-------------------------------

You are probably so lost. Good. I got you focused and wandering how you are going to learn this. Yes... its all part of my plan. Take a break, eat a donut, drink some water, make a fool of yourself in front of the hot neighbor for a while, you're gonna be here for a while.

Let me say this one thing. IF YOU HAVEN'T BEEN PAYING ATTENTION, you'll probably be thinking that the objects I'm referring are like chairs and tables and couches.. NO. WRONG. YOU ARE AN IDIOT! Pay attention from now on or I'll have to type in all caps... Yes, someday I'll learn how to make bold text and you're all doomed! Fine, learning to make an object. First lets make the OBJECT FUNCTION.

grease(value1, value2, value3)
{
}

Yay! Thats it! If I remember right atleast (hmmmm). You're probably wandering why put things in those () eh? Well, they are used for objects! HA! Thats what they are used for! You finally know! Anyways, we're not done yet. Lets first have a purpose for our object.. say we want it to take two numbers and subtract them. We'll need to make it look something like this:

grease(value1, value2)
{
this.firstnumber = value1
this.secondnumber = value2
this.finalproduct = value1 - value2
}

There, no a variable will automatically be subtracted when made.. how handy and dandy! This is only a basic object (i'm hoping) so there more to it. BUT WAIT! We've only made the object's instructions... THough we have the cake instructions, we don't have the cake yet! We need to bake the cake.. so lets make our object.

First find anywhere besides in the grease function. Like your main() function or something. Whatever. Put this in it.

var warcraft3 = grease(5, 3)

There.. simple as that... now to get our final product, we can just type ANYWHERE after var warcraft3 was made and get it! Just type the follow AFTER we've made that variable (either after it in the function, or in some function that will run after the function warcraft3 was made in).

warcraft3.finalproduct

Thats it. The engine will see that and replace it with 2 (since 5 - 3 = 2). Say we wanted to do it again. We could do something like this:

var nailgun = grease(89, 89)

Obviously nailgun.finalproduct would equal 0. If you don't understand, practice this yourself. Make a function with all those extra bits, then do the variable stuff. If it works, you should get the correct number!



----------------
4. THE REVIEW
----------------

1. What makes Javascript, C++, etc so great?

2. What is an object?

3. What is one reason we like objects?

4. Whats the format for an object (function wise)

5. How do you set a variable to be an object?

6. When you tested it, did your test work?

********
*******
******
*****
****
***
**
*
**
***
****
*****
******
*******
********
*********
********
*******
******
*****
****
***
**
*
**
***
****
*****
******
*******
********
********
*******
******
*****
****
***
**
*
**
***
****
*****
******
*******
********
*********
********
*******
******
*****
****
***
**
*

Q. What makes Javascript, C++, etc so great?
A: OBJECTS SILLY!

Q. What is an object?
A: Basically a variable that can have function powers (scripting powers)

Q. What is one reason we like objects?
A: Its pretty or it has scripting powers

Q. Whats the format for an object (function wise)
A: objectname(value1, value2, value3, value4, etc)
{
}

Q. How do you set a variable to be an object?
A: var variable_name = objectname(value1, value2, value3, value4, etc)

Q. When you tested it, did your test work?
A: If it did, good job, if it didn't, try try and try again!



-----------------
5. CONCLUSION
-----------------

Yay, hopefully you understand the hardest part of scripting (basically). I made it easy to understand (I hope). But then again I didn't say even 2% of the things objects can do (I think), so explore with them. If you can't do something, its more than likely because you don't know how to do it with objects! Plus, if you learn these, you will learn C++ a ton easier!

-by blue_moogle