I need this structure to organize my code…so that I can call it like this
Control.Menu
Here is my attempt:
var Control =
{
Menu : menu
{
menu_timer: 0,
menu_element: 0,
top_mouse_over: function ( id )
{
Menu.bottom_mouse_over();
Menu.menu_element = document.getElementById( id );
Menu.menu_element.style.visibility = 'visible';
},
...
}
};
This is your problem:
What’s the
menuon the right side of the colon for? All you need is this:Inside
top_mouse_over(),Menuwon’t be defined:…it was just a property on the
Controlobject; you never created aMenuvariable anywhere. Instead, you can refer to it asControl.Menu, or use thethiskeyword. In a function,thisis whatever object the function was called as a property of, i.e. the thing to the left of the dot. In this case, if you call it like this:…then inside
top_mouse_over,thiswill point toMenu. You could change the body of the function to this:(If you’re not familiar with
this, please read up on it before you use it. Until you understand it, it will be easy to make mistakes.)FWIW, common convention is to capitalize only constructors (functions you call with the
newkeyword), and to start anything else with a lowercase letter.