Sorry, copy-and-paste error on the entire foo() function introducing
not one but two errors; it should be:

function foo(stringArray)
{
    for (i = 0; i < stringArray.length; ++i)
    {
        bar(stringArray[i]);
    }

}

-- T.J. :-)

On Jun 8, 10:22 am, "T.J. Crowder" <[EMAIL PROTECTED]> wrote:
> Hi Fred,
>
> No, not in standard JavaScript.  The interpreter will work its way up
> the scope chain looking for "Prototype"; if it reaches the global
> object (which is always the root of the scope chain; the global object
> in browser apps is the window object), it assumes the reference is a
> property of that object.  The property will have the value undefined.
>
> That's why people keep getting into trouble with implicit global
> variables (e.g., forgetting to declare them), like this:
>
> function foo(stringArray)
> {
>     for (i = 0; i < myArray.length; ++i)
>     {
>         bar(stringArray);
>     }
>
> }
>
> function bar(element)
> {
>     i = element.indexOf(',');
>     if (i > 0)
>     {
>         $(element.substring(0, i)).update(element.substring(i + 1));
>     }
>
> }
>
> Since 'i' isn't declared anywhere, it implicitly becomes a property of
> the global object (window), and hence the above code blows up because
> the 'i' in foo() and the 'i' in bar() are the same property
> (window.i), and so bar() stomps on the array iterator in foo().
>
> More in my sadly-neglected 
> blog:http://blog.niftysnippets.org/2008/03/horror-of-implicit-globals.html
> --
> T.J. Crowder
> tj / crowder software / com
>
> On Jun 7, 6:00 pm, "Frederick Polgardy" <[EMAIL PROTECTED]> wrote:
>
> > I thought this would throw an exception if Prototype wasn't defined.  You
> > would need to say:
>
> > var Prototype
> > if (Prototype) { ... }
>
> > Right?
>
> > -Fred
>
> > On Sat, Jun 7, 2008 at 1:58 AM, T.J. Crowder <[EMAIL PROTECTED]> wrote:
>
> > > Or "if (Prototype) { ... }" ?
> > > --
> > > T.J. Crowder
> > > tj / crowder software / com
>
> > > On Jun 6, 10:12 pm, kangax <[EMAIL PROTECTED]> wrote:
> > > > typeof unfortunately returns "object" when applied to null
> > > > typeof null; // "object"
>
> > > > A safer way is to test for undefined:
>
> > > > if (typeof Prototype != 'undefined') {
> > > >   ...
>
> > > > }
>
> > > > - kangax
>
> > > > On Jun 6, 4:35 pm, nlloyds <[EMAIL PROTECTED]> wrote:
>
> > > > > On Jun 4, 1:50 pm, sheps-ii <[EMAIL PROTECTED]> wrote:
>
> > > > > > Thank you, that's fantastic. I love how prototype helps true
> > > > > > modularity into content(server side)->structure (html)->behaviour
> > > > > > (js). Cheers!
>
> > > > > To take it a step further, I usually encapsulate everything in a class
> > > > > or namespace. So, the bottom of the html looks like this:
>
> > > > > <script type="text/javascript" src="prototype.js"></script>
> > > > > <script type="text/javascript" src="mysite.js"></script>
> > > > > </body>
> > > > > </html>
>
> > > > > Then the mysite.js:
>
> > > > > if (typeof Prototype == "object") { // only load if Prototype is
> > > > > present
> > > > >   var MySite = Class.create({
> > > > >     initialize : function () {
> > > > >      ... // stuff to do on page load
>
> > > > >      this.addObservers();
> > > > >     },
>
> > > > >     // Add additional event handlers here
> > > > >     addObservers : function () {
> > > > >       ...
> > > > >     },
>
> > > > >     ...
>
> > > > >   });
> > > > >   document.observe("dom:loaded", function () { new MySite() });
>
> > > > > }
>
> > > > > I don't know if that's the best way to do things, but It works well
> > > > > for me. YMMV
>
> > --
> > Science answers questions; philosophy questions answers.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to