Ok... I've done a lot of reading on how to create objects, and I understand most of it, except the prototype part... Seems to me it's just a way to inherit classes (i'm natively a VC++ person)... I don't want to inherit anything, just create a single class...
Here's my problem: I have an object i'm creating that will generate a "floating menu" (one that will be movable by the mouse).. But I want to create more than one easily, that's why I'm using classes. However, I have 2 functions that REQUIRE me to use my variable's name, rather than the common "this" member.... If i were to use just one single menu, this would not bother me.. however, I want to use maybe 5 or 6 movable menu's (maybe changed to movable web sections later)...
**I've created my functions internal to the constructor as to make them "private", and only accessible as a member of the object. (with the exception of rememberMenuTop() because I ran into this problem, and didn't want to change much more until I fixed it)**
As you can see below, (for example) in my this.MenuMove function, i'm having to use "myMenu.rememberTop" instead of "this.rememberTop"... The only variable that seems to actually work there with a "this." is "this.object".. i've added a line above "(myMenu.rememberTop) ? rememberMenuTop():0;", and it was a "self.status=....." which displayed every single variable in my class... EVERYTHING comes up as "undefined", when it's clearly defined above.... Now, using myMenu.variableName, it's worked flawlessly; but we're aware of why I don't want this...
One last thought - I originally was thinking it was because those functions requiring the "myMenu.variableName" were called by a Mouse Event Capture, and for some strange reason it was requiring an external variable reference. The advocate to that is that I can use "this.object" in those functions just fine, it's just other variables that don't work correctly.
I've been racking my brain to figure it out, and the only information I can find about creating objects were extremely simple, and of no use to me or my problem. On the other hand, I found a really nice page about prototypes, which made me redesign it from scratch, but I ended up having trouble getting it to work at all.. So I'm back to this version (thank goodness for RCS's)
[PS - I'm aware this will only work for IE5+, but that's ok for now]
Any ideas?? And a big THANK YOU to anyone who can help
//////////////////////////
//////////
//////////
//////////
//////////
//////////
//////////
//////////
//////////
/
// This is a javascript "class" for "myMenu"
// Variables needed to define in HTML are as follows:
// - myMenu.menuName: String, Menu name you want to display
// - myMenu.link[]: String, Format="Menu Text|URL|Info Text",
// Index=0 to Infinity
// - myMenu.renderMenu(): Nothing to define, Make sure it comes
// AFTER link definitions
//////////////////////////
//////////
//////////
//////////
//////////
//////////
//
//Auto-Include CSS for menu stuff
document.write('<link rel="stylesheet" type="text/css" href="menu.css" />');
//////////////////////////
//////////
//////////
//////////
//////////
//////////
//
//////////////////////////
//////////
//////////
//////////
//////////
//////////
//
// Class Definition
function menu()
{
//Variables (private?)
this.lastTop = 10;
this.over = 0;
this.rememberTop= 0;
this.object = null;
this.menuName = "Menu Name";
this.link = new Array();
//Functions (public?)
this.init= function init()
{
document.onmousedown = this.MenuDown;
document.onmousemove = this.MenuMove;
document.onmouseup = this.MenuUp;
};
// Menu DOWN capturing
this.MenuDown= function ()
{
if (myMenu.over)
{
this.object = document.getElementById("p
anel").sty
le;
X=event.offsetX; //Offsets in object
Y=event.offsetY;
}
};
// Menu MOVE capturing
this.MenuMove= function ()
{
(myMenu.rememberTop) ? rememberMenuTop():0;
if (this.object)
{
this.object.left = event.clientX-X + document.body.scrollLeft;
this.object.top = event.clientY-Y + document.body.scrollTop;
myMenu.lastTop=event.clien
tY-Y;
return false; //So we don't highlight text on accident
}
};
this.sticky=function (obj)
{
this.rememberTop=!this.rem
emberTop;
(this.rememberTop) ? obj.style.backgroundColor=
"green" : obj.style.backgroundColor=
"red";
};
this.MenuUp=function()
{this.object = null;};
this.displayInfo= function (i)
{
infobar = document.getElementById("i
nfo"); //Find my infobar
if (i == "sticky") //If I'm referring to STICKY info
infobar.innerHTML="Sticky:
" + this.rememberTop;
else //Otherwise, it's a real link
{
sublink = this.link[i].split("|"); //Split the link[] into an array
infobar.innerHTML = sublink[2]; //infobar = Link Text (sublink[2])
}
};
this.wipeInfo=function ()
{
infobar = document.getElementById("i
nfo");
infobar.innerHTML = "<br>";
};
this.renderMenu=function ()
{
document.write('<div id="sticker" onMouseOver="myMenu.displa
yInfo(\'st
icky\');"'
+'onMouseOut="myMenu.wipeI
nfo()"'
+'onClick="myMenu.sticky(t
his); myMenu.displayInfo(\'stick
y\');"></d
iv>');
//Create Menu Header [move ID]
document.write("<div id='move' onmouseover='myMenu.over=t
rue' onmouseout='myMenu.over=fa
lse'>"
+"<font color=red><b>"+ this.menuName +"</b></font></div>");
document.write("<CENTER>")
;
//Display each link (classify, link, mouseOver/Out events, link text)
for(i=0;i<this.link.length
;i++)
{
sublink = this.link[i].split("|");
document.write("<a href='" + sublink[1] + "' class='menu' onmouseover='myMenu.displa
yInfo(" + i + ")' "
+"onmouseout='myMenu.wipeI
nfo()'>"
+ sublink[0] + "</a>");
}
document.write("</CENTER><
BR><div id='info'><br></div>");
};
}
function rememberMenuTop()
{
document.getElementById("p
anel").sty
le.top = document.body.scrollTop+my
Menu.lastT
op;
}
//////////////////////////
//////////
//////////
//////////
//////////
//////////
//
// Declare myMenu as a menu() class
var myMenu=new menu();
myMenu.init();
Start Free Trial