Find out a method’s signature on the Firebug console
by Wolfram Kriesing

“Oh cool, I found this function in dojo that I was already looking for, but how do I call it?”

Remember saying that? I do!

A lot of functionality where I am not sure how it’s going to work exactly I try out in Firebug. It’s just great to verify that it works before I have to debug. But sometimes a function slips in that I don’t know so well by heart (lately it was the dojox.lang.functional package I was playing with).
Let’s say I have found dojo.indexOf which probably returns the index of some value inside an array, string or whatever. But how are the parameters I have to pass exactly? Well I can look it up in the API browser (in progress), but switching back to the browser and searching stops the flow I am in. So I can do the simple trick on the FireBug console, just add .toString() to the method:

>>> dojo.indexOf.toString()
"function (array, value, fromIndex, findLast) {
    var step = 1, end = array.length, i = 0; 
    if (findLast) { 
        i = end - 1; 
        step = end = -1; 
    } 
    for (i = fromIndex || i; i < end; i += step) {
        if (array[i] == value) { 
            return i; 
        } 
    } 
    return -1; 
}"

Hehe, cool, ha? Now I can see there are four parameters that this method expects, since they are nicely named I have no difficulties knowing what they mean. Now I can use dojo.indexOf right. Easy.
That even has the advantage I can look into it and see how it works.

Put the pieces together, use the TAB key to find the right method you need and then inspect it using toString() and you are getting close to not leaving Firebug anymore at all. Effeciency rules!

Have fun exploring instead of searching!

Tags: , , , ,

This entry was posted on Friday, March 7th, 2008 at 3:18 pm and is filed under Dojo Cookies. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

6 Responses to “Find out a method’s signature on the Firebug console”

  1. pottedmeat Says:

    Unfortunately, this won’t work with every Dojo function. If using this technique results in some code that looks nothing like what you expected, you’ll have to instead consult the API or source code (it was likely created using dojo.declare)

  2. rcoup Says:

    hmmm, wonder if we could look up the API reference magically? Firebug does support extensions, or we could do something like python: dojo.help(dojox.foo.bar)…

  3. mccain Says:

    That is a really good idea, dojo.help()! Great thing, some extension in firebug may later wrap it and provide the help via tooltips or alikes! Cool idea.
    May be it could try to get the api docs first using the current page (so you get exactly the version you are using) and fallback to online getting it form dojotoolkit … appealing idea!

  4. pottedmeat Says:

    @rcoup and @mccain

    This is something I’ve been thinking about for a while.

    @rcoup I’ll try to set something like this up. The question is how to display it, whether to just have it return a string, or pop up a dialog of some sort.

    @mccain It would likely be more expensive to parse the current page than to just hit our API RPC. dojo.version should allow us to grab the most up-to-date documentation for that version.

  5. rcoup Says:

    @pottedmeat

    We’re using markdown for API docs, and it’s pretty readable in plain text. If a Firebug extension happened it could want it as a HTML snippet. Could alternatively do it as a Sidebar page in FF - might be more readable.

  6. mccain Says:

    the idea about using the API on the console (currently only Firebug) is circling in my head now for a while and I turn it forth and back and I got this idea.
    Couldn’t we write a mixin, that one could mix in to the entire dojo-namespace, which allows to write something like:
    dojo.forEach.help()
    so that we are actually builind something like the toString() just really helpful.
    Why?
    Because this way teh autocompletion of the method stays intact, whereas dojo.help(”dojo.forEach”) would not allow to use the auto completion. That is actually the main reason why I cam eup with it.
    Thinking…

Leave a Reply

You must be logged in to post a comment.