T.J. Crowder wrote:
> Hi Bill,
> If I'm reading you right, you want to pass a callback into another
> function. Functions are first-class objects in JavaScript so there's
> nothing special you need to do (and certainly no need for eval).
> Say you have a logging function:
>     function logMsg(msg) {
>         alert(msg); // Obviously not realistic
>     }
> That function is just an object with a name ("logMsg"). The name is
> valid throughout its containing scope. (If the declaration above is at
> page level, its containing scope is the page; if it's within another
> function, its containing scope is that function.)
> Now suppose you want to pass that logging function into another
> function to be used for logging messages. The other function might
> look like this:
>     function doSomething(logger) {
>         logger("This is message one.");
>         logger("This is message two.");
>         // ...
>     }
> To do that, just pass it into the function using its name (no quotes):
>     doSomething(logMsg);
> Note that we're just *referring* to the `logMsg` function, not calling
> it. You don't want to do this:
>     doSomething(logMsg()); // <= WRONG
> ...because (as you know) that would _call_ `logMsg` and then pass its
> return value into `doSomething`.
> The concept of functions as proper objects is very powerful. We can
> pass functions into other functions (as above). We can assign
> functions to variables:
>     var f = logMsg;
>     f("Log this message");
> We can attach functions to objects as properties:
>     var x = {};        // Create an empty object
>     x.logger = logMsg; // Create a property `logger` referencing our
> function
>     x.logger("Foo");   // Use it to log "Foo"
> You'll recognise some of this from the Prototype API. For instance,
> Enumerable#each accepts a function as a parameter and calls it for
> each item in the enumerable. As Alex mentioned, the Ajax stuff accepts
> an options object that has properties with certain defined names
> ("onSuccess", "onComplete"); the Ajax stuff expects the values of
> those properties to be a function to call.
> When passing functions around, there's something you have to remember:
> The *context* (the `this` value) within a function call is set by how
> you *call* the function, not (as it is in several other languages) by
> how/where you define the function. If `this` is going to be
> significant in terms of your callback, I'd suggest reading these posts
> [1] from my (pathetic little) blog.
> [1] http://blog.niftysnippets.org/2008/03/mythical-methods.html
> [2] http://blog.niftysnippets.org/2008/04/you-must-remember-this.html
> HTH,
> --
> T.J. Crowder
> Independent Software Consultant
> tj / crowder software / com
> www.crowdersoftware.com
As an advanced javascript neophyte  i very much appreciate the extended 
reply and the references.  It will take me a few days/weeks to digest 
and play with this.
Thank you very much.


Bill Drescher
william {at} TechServSys {dot} com


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-scriptacul...@googlegroups.com.
For more options, visit this group at 

Reply via email to