简体   繁体   中英

How can should I pass two functions as parameters, both can be optional, in javascript

I want to build up an flexible small API to backend for the client. To provide some convenience in using default success-/error- handlers or writing own ones, I planned to support the user with the option to pass both handlers one or none. I tried as follows.

Snippet of file "API" to backend:

function someRPCcall(method, url, data, successHandler, failedHandler) {

 // checking if successHandler and failedHandler are defined and passed functions

if (paramType1 === 'undefined' || paramType1 === 'null') {
            successHandlerHelper = defaultSuccessRESTHandler;
        } else if (paramType1 === 'function') {
            successHandlerHelper = successHandler;
        } 
if (paramType2 === 'undefined' || paramType2 === 'null') {
            failedHandlerHelper = defaultFailedRESTHandler;
        } else if (paramType2 === 'function') {
            failedHandlerHelper = failedHandler;
        }

        ajaxCall(method, url, data, successHandlerHelper, failedHandlerHelper);
}

  function ajaxCall(method, url, data, success, failed) {
        console.log("in ajaxCcall");
        $.ajax({
            type: method,
            contentType: "application/json",
            data: data,
            url: url,
            success: success,
            error: failed
            dataType: "json"
        });
        console.log("ajaxCall - call done");
    }

Snippet of file of client code

someRPCcall will be indirectly called by functions in another file:

someFunctionRPCcall("bla", null, errorHandler);

whereas searchWordOccurrenceRPCcall is calling someRPCcall

Above the failureHandler would be a selfdefined Handler, but following calls also should be possible:

someFunctionRPCcall("bla", null, successHandler, errorHandler);
someFunctionRPCcall("bla", null, successHandler);
someFunctionRPCcall("bla", null);

I heard about taking an object, in which the functions would be defined....

This code does not call the self defined handlers, but I guess this is another problem (using apply is missing or something like that) The question I wanted to put here is: Is there a way to pass both functions as optional parameters? How about that proposal useing objects?

Thanks

As you mentioned, you can get your function to take an object, eg:

function someRPCcall(args) {
    var method = args.method || "POST";
    var url = args.url || "default";
    var data = args.data || {};
    var successHandler = args.success || defaultSuccessRESTHandler;
    var failedHandler = args.failed || defaultFailedRESTHandler;

    ajaxCall(method, url, data, successHandler, failedHandler);
}

You will notice above that if a property is not present on the args object then it is given a default.

Then call it as follows:

someRPCcall({
    url : "the url",
    failed: function() { ... }
});

Of course, you could throw an exception if some values are not set, eg url :

function someRPCcall(args) {
    if(!args.url) { 
        throw "url must be set";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM