Closed Thread Icon

Topic awaiting preservation: Going Object-oriented in Javascript Pages that link to <a href="https://ozoneasylum.com/backlink?for=8402" title="Pages that link to Topic awaiting preservation: Going Object-oriented in Javascript" rel="nofollow" >Topic awaiting preservation: Going Object-oriented in Javascript\

 
Author Thread
Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 11-22-2002 07:53

I'm trying to design a 'Galagan-style' space shooter in Javascript to cure
the mind-numbing hours at work. I've failed at trying to use arrays to keep
track of missile numbers, positions, velocities, and the like...

... I've come to the realization that turning to object-oriented techniques
is the most efficient recourse I've got. Thing is, I've no idea how to pull
it of off in Javascript- I doubt I can do something as 'C-like' as pulling
in a 'struct'.

Any idea how to work object-oriented in Javascript?


Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

Rooster
Bipolar (III) Inmate

From: the uterus
Insane since: Nov 2002

posted posted 11-22-2002 08:38

No speaka englass, only memory pointer.....

code:
<script language="javascript">
<!--
function className() {
this.data = 0;
this.getData = function() {
return this.data;
}
this.setData = function(val) {
if (val) {
this.data = val;
}
}
this.alertData = function() {
alert(this.data);
}
}
//
objectOne = new className();
objectOne.alertData(); //alerts, "0".
objectOne.setData(5);
objectOne.alertData(); //alerts, "5".
//
objectTwo = new className();
objectTwo.objData = "C++ rocks my nutties";
//
alert(objectTwo.objData); //alerts,"C++ rocks my nutties".
//
if (objectOne.objData) {
alert("JavaScript rocks"); //Sorry, that just isn't a factual statement.
}
//
className.prototype.classData = "prototype is a special token";
//
alert(objectOne.classData); //alerts, ^.
alert(objectTwo.classData); //dido.
//-->
</script>




[edit]No blank spaca in code tag.[/edit]

..
~Existence is a mere pattern.~

[This message has been edited by Rooster (edited 11-22-2002).]

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 11-22-2002 10:09

Petskull: great idea! I recently did a BoulderDash on my laptop to kill the time I was loosing in my the public transports.

Rooster: just missed to mention that you can pass some arguments in the constructor, but I guess you'd have found it by yourself

BTW, for some clarity reasons, I prefer to assign all the methods handlers first and then to write the nested functions/methods. see the above syntax:

code:
<script language="javascript">

function className( data )
{
// member variables
this.data = data
//
// methods handlers
this.alertData = className_alertData
this.doNothing = className_doNothing
//
// methods declarations
function className_alertData( alertCaption )
{
alert( alertCaption +"\n"+ this.data )
}
//
function className_doNothing( )
{
// that's an extremely usefull function
}
}

</script>



Cheers,

Mathieu "POÏ" HENRI

[This message has been edited by poi (edited 11-22-2002).]

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 11-22-2002 15:09

Once you've created a class as the others have explained, you can make an instance of it as follows:

my classinstance = new className();

(Note that the "new" operator has nothing to do with the "new" operator in C++. Here, it runs the function as a constructor, and then returns the object the function created. No pointers involoved.)

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 11-22-2002 18:37
quote:
alert("JavaScript rocks"); //Sorry, that just isn't a factual statement.



*snicker*


Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

« BackwardsOnwards »

Show Forum Drop Down Menu