On May 24, 10:24 am, Michael <mich...@michaelminella.com> wrote:
> I understand how Prototype works with regards to the removal of
> <script> tags after evaling the results of an Ajax request.  However,
> I was doing some research and am now starting to wonder why the way I
> declare functions works.
> According to the Prototype documentation, you need to declare the
> function and assign it to a global variable:
> myFunction = function() {alert('hi');}

It's usually a good idea to avoid undeclared assignments (for clarity/
compatibility/robustness). Instead, consider assigning to a `window`
property directly -

window.myFunction = function(){ ... };

- or if you're extra cautious about assigning to an unpredictable and
not-necessarily existent host object (which `window` happens to be),
assign to an actual Global object -

var global = (function(){return this;})();
global.myFunction = function(){ ... };

> That makes sense.  However, in all of my scenarios, I've declared
> functions like this:
> var myFunction = function myFunction() {alert('hi');}

Named function expressions, which you're using here, have their own
quirks across browsers. Be careful with them.

> and the calls to myFunction work just fine.  My question is...why does
> my way work?  According to the Prototype documentation, the local
> variable myFunction should be thrown away after the eval.  Any insight
> anyone can provide would be appreciated.  Thanks in advance!

`myFunction` probably leaks into a global scope from within somewhere
else. There's no other way for it to be declared globally, as long as
`eval` (used internally by `evalScripts`) is spec-compliant and so
evaluates in the scope of its caller - an internal anonymous function
in case of Prototype.js.

You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
For more options, visit this group at 

Reply via email to