7/19/20 by DW
Dave Winer asked about simplifying such JavaScript code, used a lot for callbacks:
if (callback !== undefined) {
callback ();
}
Here's a suggestion. Processing could be moved to a helper function, shortening the call:
function callbackIf (
cb
) {
if (cb !== undefined) cb()
}
// Usage
callbackIf(callback)
To pass data to the callback I would extend the helper to accept a variable number of arguments and pass them through:
function callbackIf (
cb,
...args
) {
if (cb !== undefined) cb(...args)
}
// Usage
callbackIf(callback)
callbackIf(callback, 'Hi', 'there')