473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Re-coding from structured to OOP code

RMWChaos
137 100+
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.

Expand|Select|Wrap|Line Numbers
  1. /*
  2. * capitalize() does exactly what it says
  3. * mouseHover() is a function that swaps images onmouseover & onmouseout
  4. * removeDOM() removes a specified DOM node
  5. * createDOM() creates a DOM node from the supplied attrib list
  6. * loadPage() clears the old page data and loads the new
  7. *
  8. * The following code iterates through a list of webpage
  9. * names and creates a nav button for each page. The
  10. * function of each nav button changes depending on
  11. * the page selected (i.e. onclick, onmouseover, etc.)
  12. */
  13.  
  14. // default required attribs
  15. var attribDefaults =
  16.     {
  17.     'dom'   :  'div',
  18.     'parent':  'body'
  19.     };
  20.  
  21. // exception list for dom creation via JS or DOM methods
  22. var exceptions =
  23.     {
  24.     'onclick'    :  0,
  25.     'onmouseover':  1,
  26.     'onmouseout' :  2
  27.     };
  28.  
  29. // The webpage names
  30. var pageName =
  31.     {
  32.     'home'   :  0,
  33.     'guides' :  1,
  34.     'specs'  :  2,
  35.     'links'  :  3,
  36.     'join'   :  4,
  37.     'news'   :  5,
  38.     'forum'  :  6,
  39.     'kills'  :  7,
  40.     'corp'   :  8,
  41.     'members':  9
  42.     };
  43.  
  44. // Initiate navigation buttons creation
  45. function loadNav(page)
  46.     {
  47.     for (var index in pageName)
  48.         {
  49.         // vars change navigation object below for each page
  50.         var imageId = "nav" + index;
  51.         var capital = index.capitalize();
  52.         var altName = "Jump to " + capital + " sector";
  53.         var divNavExist = document.getElementById(imageId);
  54.         var pathHover = "../images/hover" + capital + ".jpg";
  55.         var getPage = new Function('loadPage("' + index + '")');
  56.         var pathDefault = "../images/default" + capital + ".jpg";
  57.         var pathSelected = "../images/selected" + capital + ".jpg";
  58.         var navAlert = new Function("alert('You are already here.')");
  59.         var alreadyHere = 'Your NavCon indicates that you are already here.';
  60.         var mouseOver = new Function("mouseHover('" + pathHover + "', '" + imageId + "')");
  61.         var mouseOut = new Function("mouseHover('" + pathDefault + "', '" + imageId + "')");
  62.         // The list of DOM node attributes for the nav buttons
  63.         var navigation =
  64.             {
  65.             'dom'        :  'img',
  66.             'parent'     :  'body',
  67.             'id'         :  imageId,
  68.             'alt'        :  [altName, alreadyHere],
  69.             'title'      :  [altName, alreadyHere],
  70.             'src'        :  [pathDefault, pathSelected],
  71.             'onmouseover':  [mouseOver, null],
  72.             'onmouseout' :  [mouseOut, null],
  73.             'onclick'    :  [getPage, navAlert]
  74.             };
  75.          // default to first value
  76.         var nav = 0;
  77.         // if button is for the selected page, use second value
  78.         if (index == page)
  79.             {
  80.             nav = 1;
  81.             };
  82.         // remove old nav buttons
  83.         if (divNavExist)
  84.             {
  85.             removeDOM(imageId);
  86.             };
  87.         // create new nav buttons
  88.         var attribList = {};
  89.         for (index in navigation)
  90.             {
  91.             var value = navigation[index]; 
  92.             attribList[index] = value instanceof Array ? value[nav] : value;
  93.             };
  94.         // submit button attribs to createDOM()
  95.         createDOM(attribList);
  96.         };
  97.     };
  98.  
My first thought is that I need to start with something like this:

Expand|Select|Wrap|Line Numbers
  1. function NavButton(page)
  2.     {
  3.     this.attribDefaults =
  4.         {
  5.         'dom'        :  'div',
  6.         'parent'     :  'body'
  7.         };
  8.     // exception list for dom creation via JS or DOM methods
  9.     this.exceptions =
  10.         {
  11.         'onclick'    :  0,
  12.         'onmouseover':  1,
  13.         'onmouseout' :  2
  14.         };
  15.     // The webpage names
  16.     this.pageName =
  17.         {
  18.         'home'   :  0,
  19.         'guides' :  1,
  20.         'specs'  :  2,
  21.         'links'  :  3,
  22.         'join'   :  4,
  23.         'news'   :  5,
  24.         'forum'  :  6,
  25.         'kills'  :  7,
  26.         'corp'   :  8,
  27.         'members':  9
  28.         };
  29.     for (var index in this.pageName)
  30.         {
  31.         this.index = index;
  32.         };
  33.     this.page = page;
  34.     this.imageId = "nav" + this.index;
  35.     this.capital = this.index.capitalize();
  36.     this.altName = "Jump to " + this.capital + " sector";
  37.     this.divNavExist = document.getElementById(this.imageId);
  38.     this.pathHover = "../images/hover" + this.capital + ".jpg";
  39.     this.pathDefault = "../images/default" + this.capital + ".jpg";
  40.     this.pathSelected = "../images/selected" + this.capital + ".jpg";
  41.     this.alreadyHere = 'Your NavCon indicates that you are already here.';
  42.     };
  43.  
  44. NavButton.prototype.page = 'Current Page';
  45. NavButton.prototype.removeOldDom = function ()
  46.     {
  47.     if (divNavExist)
  48.         {
  49.         removeDOM(imageId);
  50.         };
  51.     };
  52. NavButton.prototype.getPage = function()
  53.     {
  54.     loadPage(index);
  55.     };
  56. NavButton.prototype.navAlert = function()
  57.     {
  58.     alert('You are already here.');
  59.     };
  60. NavButton.prototype.mouseOver = function()
  61.     {
  62.     mouseHover(pathHover, imageId);
  63.     };
  64. NavButton.prototype.mouseOut = function()
  65.     {
  66.     mouseHover(pathDefault, imageId);
  67.     };
  68. NavButton.prototype.createNav = function()
  69.     {
  70.     createDOM(attribList);
  71.     };
  72. NavButton.prototype.mouseHover = function(imagePath, imageId)
  73.     {
  74.     var image = document.getElementById(imageId);
  75.     image.src = imagePath;
  76.     };
  77.  
  78. String.prototype.capitalize = function()
  79.     {
  80.     return this.replace(/\w+/g, function(a)
  81.         {
  82.         return a.charAt(0).toUpperCase() + a.slice(1).toLowerCase();
  83.         });
  84.     };
  85.  
  86. var home = new NavButton('home');
  87. var guides = new NavButton('guides');
  88. var specs = new NavButton('specs');
  89. var links = new NavButton('links');
  90. var join = new NavButton('join');
  91. var news = new NavButton('news');
  92. var forum = new NavButton('forum');
  93. var kills = new NavButton('kills');
  94. var corp = new NavButton('corp');
  95. var members = new NavButton('members');
  96.  
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
Dec 24 '07 #1
2 1321
RMWChaos
137 100+
Expand|Select|Wrap|Line Numbers
  1. var home = new NavButton('home');
  2. var guides = new NavButton('guides');
  3. var specs = new NavButton('specs');
  4. var links = new NavButton('links');
  5. var join = new NavButton('join');
  6. var news = new NavButton('news');
  7. var forum = new NavButton('forum');
  8. var kills = new NavButton('kills');
  9. var corp = new NavButton('corp');
  10. var members = new NavButton('members');
  11.  
Immediately, I see some redundancy in the code. I would want to create a new function that automatically created these vars above, like this:

Expand|Select|Wrap|Line Numbers
  1. function createNavVars()
  2.     {
  3.     for (var i = 0; i < pageName[i].length; i++)
  4.         {
  5.         var pageName[i] = 'new NavButton(' + pageName[i] + ')';
  6.         };
  7.     };
  8.  
Or something to that effect. So how would that fit into the whole OOP model?

Man, this can get so convoluted. :-\
Dec 24 '07 #2
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:

Expand|Select|Wrap|Line Numbers
  1. function OBJ() {
  2. }
  3.  
  4. function create_items() {
  5.     for (var i = 0; i < 3; i++) {
  6.         this['a' + i] = new OBJ;
  7.     }
  8. }
  9.  
  10. create_items();
  11.  
  12. for (var i in window) {
  13.     if (window[i] instanceof OBJ) {
  14.         alert(i);
  15.     }
  16. }
  17.  
Dec 24 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
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...
4
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...
11
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"; }
4
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...
8
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...
8
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...
1
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 ...
2
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...
1
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',
1
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...
0
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,...
0
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$) { } ...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.