Advertisement

12.17.2003 at 10:58PM PST, ID: 20829098
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.4

Creating custom objects: Variables aren't working correctly

Asked by gamerD00d in JavaScript

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("panel").style;
     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.clientY-Y;
     return false; //So we don't highlight text on accident
    }
  };

 this.sticky=function (obj)
  {
   this.rememberTop=!this.rememberTop;
   (this.rememberTop) ? obj.style.backgroundColor="green" : obj.style.backgroundColor="red";
  };

 this.MenuUp=function()
  {this.object = null;};

 this.displayInfo= function (i)
  {
   infobar = document.getElementById("info");      //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("info");
   infobar.innerHTML = "<br>";
  };

 this.renderMenu=function ()
  {
   document.write('<div id="sticker" onMouseOver="myMenu.displayInfo(\'sticky\');"'
                 +'onMouseOut="myMenu.wipeInfo()"'
                 +'onClick="myMenu.sticky(this); myMenu.displayInfo(\'sticky\');"></div>');
                 
   //Create Menu Header [move ID]
   document.write("<div id='move' onmouseover='myMenu.over=true' onmouseout='myMenu.over=false'>"
                 +"<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.displayInfo(" + i + ")' "       
                        +"onmouseout='myMenu.wipeInfo()'>"
                   + sublink[0] + "</a>");
    }
   document.write("</CENTER><BR><div id='info'><br></div>");
  };

}


function rememberMenuTop()
{
 document.getElementById("panel").style.top = document.body.scrollTop+myMenu.lastTop;
}


//////////////////////////////////////////////////////////////////////////////
// Declare myMenu as a menu() class  
var myMenu=new menu();
myMenu.init();
Start Free Trial
 
Loading Advertisement...
 
[+][-]12.17.2003 at 11:07PM PST, ID: 9962551

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12.17.2003 at 11:14PM PST, ID: 9962567

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12.17.2003 at 11:25PM PST, ID: 9962598

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12.18.2003 at 04:38AM PST, ID: 9963592

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12.18.2003 at 08:32AM PST, ID: 9965307

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12.18.2003 at 08:36AM PST, ID: 9965342

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12.19.2003 at 04:13AM PST, ID: 9971416

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12.21.2003 at 01:28PM PST, ID: 9982195

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02.18.2004 at 11:53AM PST, ID: 10395967

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.22.2004 at 10:42AM PST, ID: 10426807

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: JavaScript
Sign Up Now!
Solution Provided By: SpazMODic
Participating Experts: 4
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32