I've read through the volumes of information on MDC, Douglas Crawford's site, and many other websites on OOP, and now I am ready to try some conversion on my own code. I understand the principals (I think), but I haven't a clue where to start. So here's some of my existing code, and now I need some suggestions of where to go with this. -
/*
-
* capitalize() does exactly what it says
-
* mouseHover() is a function that swaps images onmouseover & onmouseout
-
* removeDOM() removes a specified DOM node
-
* createDOM() creates a DOM node from the supplied attrib list
-
* loadPage() clears the old page data and loads the new
-
*
-
* The following code iterates through a list of webpage
-
* names and creates a nav button for each page. The
-
* function of each nav button changes depending on
-
* the page selected (i.e. onclick, onmouseover, etc.)
-
*/
-
-
// default required attribs
-
var attribDefaults =
-
{
-
'dom' : 'div',
-
'parent': 'body'
-
};
-
-
// exception list for dom creation via JS or DOM methods
-
var exceptions =
-
{
-
'onclick' : 0,
-
'onmouseover': 1,
-
'onmouseout' : 2
-
};
-
-
// The webpage names
-
var pageName =
-
{
-
'home' : 0,
-
'guides' : 1,
-
'specs' : 2,
-
'links' : 3,
-
'join' : 4,
-
'news' : 5,
-
'forum' : 6,
-
'kills' : 7,
-
'corp' : 8,
-
'members': 9
-
};
-
-
// Initiate navigation buttons creation
-
function loadNav(page)
-
{
-
for (var index in pageName)
-
{
-
// vars change navigation object below for each page
-
var imageId = "nav" + index;
-
var capital = index.capitalize();
-
var altName = "Jump to " + capital + " sector";
-
var divNavExist = document.getElementById(imageId);
-
var pathHover = "../images/hover" + capital + ".jpg";
-
var getPage = new Function('loadPage("' + index + '")');
-
var pathDefault = "../images/default" + capital + ".jpg";
-
var pathSelected = "../images/selected" + capital + ".jpg";
-
var navAlert = new Function("alert('You are already here.')");
-
var alreadyHere = 'Your NavCon indicates that you are already here.';
-
var mouseOver = new Function("mouseHover('" + pathHover + "', '" + imageId + "')");
-
var mouseOut = new Function("mouseHover('" + pathDefault + "', '" + imageId + "')");
-
// The list of DOM node attributes for the nav buttons
-
var navigation =
-
{
-
'dom' : 'img',
-
'parent' : 'body',
-
'id' : imageId,
-
'alt' : [altName, alreadyHere],
-
'title' : [altName, alreadyHere],
-
'src' : [pathDefault, pathSelected],
-
'onmouseover': [mouseOver, null],
-
'onmouseout' : [mouseOut, null],
-
'onclick' : [getPage, navAlert]
-
};
-
// default to first value
-
var nav = 0;
-
// if button is for the selected page, use second value
-
if (index == page)
-
{
-
nav = 1;
-
};
-
// remove old nav buttons
-
if (divNavExist)
-
{
-
removeDOM(imageId);
-
};
-
// create new nav buttons
-
var attribList = {};
-
for (index in navigation)
-
{
-
var value = navigation[index];
-
attribList[index] = value instanceof Array ? value[nav] : value;
-
};
-
// submit button attribs to createDOM()
-
createDOM(attribList);
-
};
-
};
-
My first thought is that I need to start with something like this: -
function NavButton(page)
-
{
-
this.attribDefaults =
-
{
-
'dom' : 'div',
-
'parent' : 'body'
-
};
-
// exception list for dom creation via JS or DOM methods
-
this.exceptions =
-
{
-
'onclick' : 0,
-
'onmouseover': 1,
-
'onmouseout' : 2
-
};
-
// The webpage names
-
this.pageName =
-
{
-
'home' : 0,
-
'guides' : 1,
-
'specs' : 2,
-
'links' : 3,
-
'join' : 4,
-
'news' : 5,
-
'forum' : 6,
-
'kills' : 7,
-
'corp' : 8,
-
'members': 9
-
};
-
for (var index in this.pageName)
-
{
-
this.index = index;
-
};
-
this.page = page;
-
this.imageId = "nav" + this.index;
-
this.capital = this.index.capitalize();
-
this.altName = "Jump to " + this.capital + " sector";
-
this.divNavExist = document.getElementById(this.imageId);
-
this.pathHover = "../images/hover" + this.capital + ".jpg";
-
this.pathDefault = "../images/default" + this.capital + ".jpg";
-
this.pathSelected = "../images/selected" + this.capital + ".jpg";
-
this.alreadyHere = 'Your NavCon indicates that you are already here.';
-
};
-
-
NavButton.prototype.page = 'Current Page';
-
NavButton.prototype.removeOldDom = function ()
-
{
-
if (divNavExist)
-
{
-
removeDOM(imageId);
-
};
-
};
-
NavButton.prototype.getPage = function()
-
{
-
loadPage(index);
-
};
-
NavButton.prototype.navAlert = function()
-
{
-
alert('You are already here.');
-
};
-
NavButton.prototype.mouseOver = function()
-
{
-
mouseHover(pathHover, imageId);
-
};
-
NavButton.prototype.mouseOut = function()
-
{
-
mouseHover(pathDefault, imageId);
-
};
-
NavButton.prototype.createNav = function()
-
{
-
createDOM(attribList);
-
};
-
NavButton.prototype.mouseHover = function(imagePath, imageId)
-
{
-
var image = document.getElementById(imageId);
-
image.src = imagePath;
-
};
-
-
String.prototype.capitalize = function()
-
{
-
return this.replace(/\w+/g, function(a)
-
{
-
return a.charAt(0).toUpperCase() + a.slice(1).toLowerCase();
-
});
-
};
-
-
var home = new NavButton('home');
-
var guides = new NavButton('guides');
-
var specs = new NavButton('specs');
-
var links = new NavButton('links');
-
var join = new NavButton('join');
-
var news = new NavButton('news');
-
var forum = new NavButton('forum');
-
var kills = new NavButton('kills');
-
var corp = new NavButton('corp');
-
var members = new NavButton('members');
-
But that's about all I can come up with so far, and I'm not sure I even know what I am doing with what I have done. Er...yeah.
So am I at least on the right track here?
Thanks,
RMWChaos
2 1321 -
var home = new NavButton('home');
-
var guides = new NavButton('guides');
-
var specs = new NavButton('specs');
-
var links = new NavButton('links');
-
var join = new NavButton('join');
-
var news = new NavButton('news');
-
var forum = new NavButton('forum');
-
var kills = new NavButton('kills');
-
var corp = new NavButton('corp');
-
var members = new NavButton('members');
-
Immediately, I see some redundancy in the code. I would want to create a new function that automatically created these vars above, like this: -
function createNavVars()
-
{
-
for (var i = 0; i < pageName[i].length; i++)
-
{
-
var pageName[i] = 'new NavButton(' + pageName[i] + ')';
-
};
-
};
-
Or something to that effect. So how would that fit into the whole OOP model?
Man, this can get so convoluted. :-\
gits 5,390
Expert Mod 4TB
the above code wouldn't work ... to create instances you should use something like in the following example ... that create 3 instances a0 ... a2 in the window scope: - function OBJ() {
-
}
-
-
function create_items() {
-
for (var i = 0; i < 3; i++) {
-
this['a' + i] = new OBJ;
-
}
-
}
-
-
create_items();
-
-
for (var i in window) {
-
if (window[i] instanceof OBJ) {
-
alert(i);
-
}
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Nel |
last post by:
I have a question related to the "security" issues posed by Globals ON.
It is good programming technique IMO to initialise variables, even if it's
just
$foo = 0;
$bar = "";
Surely it would...
|
by: Craig Bailey |
last post by:
Anyone recommend a good script editor for Mac OS X?
Just finished a 4-day PHP class in front of a Windows machine, and liked
the editor we used. Don't recall the name, but it gave line numbers as...
|
by: James |
last post by:
My form and results are on one page.
If I use :
if ($Company) {
$query = "Select Company, Contact
From tblworking
Where ID = $Company
Order By Company ASC";
}
|
by: Alan Walkington |
last post by:
Folks:
How can I get an /exec'ed/ process to run in the background on an XP box?
I have a monitor-like process which I am starting as
'exec("something.exe");' and, of course the exec function...
|
by: Beowulf |
last post by:
Hi Guru's,
I have a query regarding using PHP to maintain a user profiles list. I
want to be able to have a form where users can fill in their profile
info (Name, hobbies etc) and attach an...
|
by: Lothar Scholz |
last post by:
Because PHP5 does not include the mysql extension any more is there
a chance that we will see more Providers offering webspace with
Firebird or Postgres Databases ?
What is your opinion ? I must...
|
by: joost |
last post by:
Hello,
I'm kind of new to mySQL but more used to Sybase/PHP
What is illegal about this query or can i not use combined query's in mySQL?
DELETE FROM manufacturers
WHERE manufacturers_id ...
|
by: sky2070 |
last post by:
i have two file with jobapp.html calling jobapp_action.php
<HTML>
<!-- jobapp.html -->
<BODY>
<H1>Phop's Bicycles Job Application</H1>
<P>Are you looking for an exciting career in the world of...
|
by: Brian |
last post by:
I have an array like this:
$events = array(
array(
'2003-07-01',
'Event Title 1',
'1' //ID Number (not unique)
),
array(
'2003-07-02',
|
by: Clarice Almeida Hughes |
last post by:
tenho um index onde tenho o link pro arq css, como sao visualizados pelo
include todas as paginas aderem ao css linkado no index. so q eu preciso de
alguns links com outras cores no css, o q devo...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |