Returning a custom command via AjaxResponse
Indeed, one way to do this is to implement CommandInterface in a new class, as described here: https://www.mike-miles.com/blog/creating-ajax-callback-commands-drupal-8 . This method adds the new command to Druapl.AjaxCommands.

However, if it's not desirable to implement the interface, a similar thing can be done with the existing InvokeCommand that implements CommandInterface, as in the example here: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Ajax%21InvokeCommand.php/class/InvokeCommand/8.2.x

My minimalistic example:

Server-side (NULL can be some CSS selector instead):

$response-&gt;addCommand(new InvokeCommand(NULL, 'customRedirect', ['/']));


Client-side:

jQuery.fn.customRedirect = function(target) {
    if (something)
        location.replace(target);
    else
        do_something_else();
};