23

Very often, I find myself using a callback function and I don't have its documentation handy, and it would be nice to see all of the arguments that are meant to be passed to that callback function.

// callback is a function that I don't know the args for...
// and lets say it was defined to be used like: callback(name, number, value)
something.doSomething( callback );

How can I determine what args its passing into that?

Note: looking at the source code can be unhelpful when the code itself is obfuscated and minified (as many js frameworks are)

3
  • 4
    arguments javascriptweblog.wordpress.com/2011/01/18/…
    – asawyer
    Mar 22, 2012 at 18:56
  • 1
    parameters should be defined by the callee, not the caller?
    – jbabey
    Mar 22, 2012 at 19:02
  • You can often look at the source code for the calling function even if you don't have the documentation handy.
    – hugomg
    Mar 22, 2012 at 19:24

4 Answers 4

34

To get the list of arguments without breaking functionality, overwrite the callback function in this way:

var original = callback;
callback = function() {
    // Do something with arguments:
    console.log(arguments);
    return original.apply(this, arguments);
};
  1. The context, this is preserved.
  2. All arguments are correctly passed.
  3. The return value is correctly passed.

NOTE: This method works in most cases. Though there are edge cases where this method will fail, including:

  • Read-only properties (e.g. defined using Object.defineProperty with writable:false)
  • Properties that are defined using getters/setters, when the getter/setter is not symmetric.
  • Host objects and plugin APIs: E.g. Flash and ActiveX.
10
  • 1
    PS. For even more details, add console.trace(); to the replaced callback function.
    – Rob W
    Mar 22, 2012 at 19:05
  • 1
    Why return original.apply(this, arguments); isn't enough?
    – gdoron
    Mar 22, 2012 at 20:43
  • 2
    @RobW: If you're talking about the note that starts Note: Most browsers, including Chrome 14 and Internet Explorer 9..., it doesn't apply to the Arguments object. I don't know of any browser that won't accept an Arguments object as the second argument to .apply(). Side note, this was recently fixed in Chrome so any Array-like object will work.
    – user1106925
    Mar 22, 2012 at 21:49
  • 1
    @amnotiam. "The note on apply doesn't apply..." The human language at it best. :-)
    – gdoron
    Mar 22, 2012 at 22:00
  • 2
    @gdoron: Indeed. :) FYI, I just verified this in IE6. The Arguments object is accepted by .apply() with no problems.
    – user1106925
    Mar 22, 2012 at 22:13
8

Could it be as easy as

function callback() {
    console.log(arguments);
}

?

Every function provides the arguments it has been called with in the automagic arguments collection.

6
  • He can't change the callback code. If he could, he wouldn't need to use the arguments to know what it's arguments...
    – gdoron
    Mar 22, 2012 at 18:58
  • Where did he say that? Since when can't you change JS code? Apart from that, a JS debugger and a breakpoint are just a few clicks away.
    – Tomalak
    Mar 22, 2012 at 19:00
  • He wrote he doesn't want to look at the documentions of the func, so for sure he doesn't have that function code handly...
    – gdoron
    Mar 22, 2012 at 19:02
  • 2
    add in console.log(this) and you have a complete view of the local(est) scope. Mar 22, 2012 at 19:03
  • this is a frequent sort of problem when you're dealing with minified / obfuscated code... like anything jquery/mootools/etc
    – Kristian
    Mar 22, 2012 at 19:03
1

Isn't this sort of the cart leading the horse?

Your function takes a callback. It's the method using your function that should be aware of what arguments the callback should accept.

1
  • 7
    that would be true if it were my function, but +1 for good use of italics. haha ;)
    – Kristian
    Mar 22, 2012 at 19:10
0

You can even tell it which function's args to get using [functionName].arguments:

(function(arg1, arg2, agr3){
    console.log('args are:', arguments);
    return function fn(){
        function m(){
            console.log(
                'fn.arguments:', fn.arguments,
                'm.arguments:', m.arguments,
                'argumentsX:', arguments
            );
        };
        m('mArg1', 'mArg2', 'mArg3', 'mArg4');
    };
})
(1, 2, Math.PI)  // invoke closure
('fnArg1', 'fnArg2', 'fnArg3', 'fnArg4');  // invoke "fn"

Every function def rewrites the the arguments keyword to be of that scope btw (as seen with the "argumentsX" log).

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.