[英]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. 我想建立一个灵活的小型API来为客户端后端。 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: 文件“ API”到后端的代码段:
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: someRPCcall将由另一个文件中的函数间接调用:
someFunctionRPCcall("bla", null, errorHandler);
whereas searchWordOccurrenceRPCcall is calling someRPCcall 而searchWordOccurrenceRPCcall正在调用someRPCcall
Above the failureHandler would be a selfdefined Handler, but following calls also should be possible: 在failureHandler之上将是一个自定义的Handler,但是以下调用也应该是可能的:
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? 这段代码没有调用自定义处理程序,但是我想这是另一个问题(使用apply缺少了类似的东西),我想在这里提出的问题是:是否可以将两个函数作为可选参数传递? 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. 您将在上面注意到,如果
args
对象上不存在属性,则会为其提供默认值。
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
: 当然,如果未设置某些值,例如
url
,则可能引发异常:
function someRPCcall(args) {
if(!args.url) {
throw "url must be set";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.