Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
147
rated 0 times [  148] [ 1]  / answers: 1 / hits: 27590  / 12 Years ago, thu, march 22, 2012, 12:00:00

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)


More From » callback

 Answers
98

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.


[#86661] Wednesday, March 21, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kinsley

Total Points: 352
Total Questions: 84
Total Answers: 94

Location: Denmark
Member since Tue, Jul 19, 2022
2 Years ago
;