Forum Moderators: open

Message Too Old, No Replies

How to create a class

         

Dabrowski

10:09 pm on May 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have lots of JS piling up, and I want to prevent duplicate function/variable names.

I know you can create classes in JS, so instead of myFunction() which may be repeated accidentally, how do I create myClass.myFunction()?

Dabrowski

10:13 pm on May 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Additionally, I for some function I might want to assign a class to an element, for example....

var myDiv = getElementById( "divToMove");

myDiv.mover = new myMovingClass();
myDiv.mover.moveTo( x, y);

I don't even know if that's feasible or possible. The object in assigning a class to an element, is so I don't have to keep track of each element within the mover class. This way the entire class would be duplicated for each element, in it's own namespace.

When I've had this situation in the past I've always used an associative array of the element ID's to track things, but this way would be more efficient.

Is it possible?

geofflee

3:31 am on May 11, 2007 (gmt 0)

10+ Year Member



Try searching Google for something that basic. A search of "javascript classes" returns this link as the first result: [webdevelopersjournal.com...]

-Geoffrey Lee

Dabrowski

7:46 pm on May 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's almost right, but the functions are still defined outside the class.

I know I could Google it, anyone could Google anything on this forum and find an answer, but what's the point of a forum if you can't ask questions?

Drag_Racer

9:17 am on May 12, 2007 (gmt 0)

10+ Year Member



function myFunc(param1,param2,param3){
this.param1=param1;
this.param2=param2;
this.param3=param3;
}

myFunc.prototype.doSomething=function(){
with(this) alert(param1+" on first, "+param2+" on second, "+param3+" on third");
}

myFunc.prototype.doSomethingElse=function(){
with(this) document.write(param1+" on first, "+param2+" on second, "+param3+" on third");
}

var myVar = new myFunc("who's","what's","I don't know's");
myVar.doSomething();
myVar.doSomethingElse();

penders

11:47 am on May 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have lots of JS piling up, and I want to prevent duplicate function/variable names.

I think may be you are refering to object literal notation and creating a global namespace...?

[webmasterworld.com...]

Another of Fotiman's code examples...
[webmasterworld.com...]

Dabrowski

12:40 pm on May 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah that's it, OLN seems to be what I want. I'm not bothered about using prototype to save memory, most of my JS's are typically less than 5k. Say 6 copies is going to create 30k more memory, who cares? IE is currently using 45Mb so this isn't going to make any difference.

So working on that theory, with my original code....

myDiv.mover = new myMovingClass();
myDiv.mover.moveTo( x, y);

My myMovingClass could use this.onClick to make itself move, but then how does the moveTo function know which element it's on, wouldn't "this" refer to mover?

Drag_Racer

1:07 pm on May 12, 2007 (gmt 0)

10+ Year Member



I posted that not so much for memory reasons, but for scope.

Dabrowski

7:42 pm on May 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, that was in reply to penders' link to Fotiman's explanation.