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)
A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action. The above example is a synchronous callback, as it is executed immediately.
A JavaScript Callback Function is a function that is passed as a parameter to another JavaScript function, and the callback function is run inside of the function it was passed into. JavaScript Callback Functions can be used synchronously or asynchronously.
No, callback should not be used with return.
Passing the event object of react as the second argument. If you want to pass a parameter to the click event handler you need to make use of the arrow function or bind the function. If you pass the argument directly the onClick function would be called automatically even before pressing the button.
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); };
this
is preserved.NOTE: This method works in most cases. Though there are edge cases where this method will fail, including:
Object.defineProperty
with writable:false
)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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With