">'); win.document.writeln(''); win.document.writeln(''); }
 

The Indefinite Article.

Tuesday, March 01, 2005

OOP in JS, Part 1 : Public/Private Variables and Methods

I think Paul turned me on to this page a while back:OOP in JS, Part 1 : Public/Private Variables and Methods It is something I keep meaning to read and just now took a look at. And as soon as I do, voila--it solves a problem for me.

(Actually, the problem may be my lack of formal JavaScript training rather than the specific problem I had, but that is another matter.)

So yesterday I was working on step 2 of the pure javascript form and I ran into a problem where Safari would lock up when the script was written a certain way. If I wrote it in a different and incorrect way it would work in Safari, but not in Firefox. I narrowed the problem to an iterator loop that referenced a function with another iterator loop. Paul gave me a hand in looking at it and found that the first loop referenced a variable named i that was also present as the iterator in the loop that was used in the first loop. So I renamed all the i's and everything worked great. This morning I read about public/private variables and methods and realized that instead of controlling the loop with (i = 0; i < formRows.length; ++i), I used (var i = 0; i < formRows.length; ++i), the i would be good only within that function. The var declaration means that this variable named i is only used in this function and not in any iterator that references it. Yahoo!

1 Comments:

  • Having encountered loops in the Action Scripting book, using 'var' to set a local variable is the 'safest' way to code ...

    i haven't made it to the practical application of loops yet, however.

    By Blogger Killy, at 3:14 PM  

Post a Comment

<< Home