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):


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
    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

T.J. Crowder
Independent Software Consultant
tj / crowder software / com

On Nov 18, 2:20 pm, bill <will...@techservsys.com> wrote:
> I suspect this is not a prototype question, but then again, every time I
> turn around I am learning new capabilities of the framework.
> I need to pass a function name as an argument to a function for a
> call-back function.
> Do I just pass the function name as a quoted text argument ?
> If I do, do I need to eval the function when I want to execute it.
> or...
> what is the best way of doing this ?
> My apologies in advance if prototype does not have a nifty way of doing
> this and my thanks in advance for any suggestions.
> --
> 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