如何在javascript中使用回调处理可选参数


//request is a node module   
var request = require('request');
/**
* send REST web service message to server
* @function sendMessage
* @param {string} method - GET, POST, PUT, DELETE
* @param {string} url - server URL
* @param {object} json - The data to be send
* @param {object} cb - callback function
*@param{object}formData-The data related to file upload
* @returns void
*/
function sendMessage(type,url,method,json,retries,formData,cb){
var options={
url:url,
formData:formData,
json:true,
switch (method) {
case 'get':
request.get(options, cb);
break;
case 'post':
envelope.token = json.token;
envelope.package = json.package;
options.body = envelope;
request.post(options, cb);
break;
case 'put':
envelope.package = json.package;
envelope.token = json.token;
options.body = envelope;
request.put(options, cb);
break;
case 'delete':
request.delete(options, cb);
break;       
}

这里sendMessage()函数已经编写好了,并且在没有FormData参数(sendMessage(type,url,method,json,retries,cb))的所有模块中都能很好地使用,但对于文件上传,我们需要使用FormData传递文件路径,而不改变所有其他函数。

如果您想要支持的两种不同的调用方案是:

sendMessage(type,url,method,json,retries,formData,cb)

这个:

sendMessage(type,url,method,json,retries,cb)

而且,回调参数总是必需的,那么,您可以这样做:

function sendMessage(type,url,method,json,retries,formDataArg,cbArg) {
var cb = cbArg, formData = formDataArg;
// if we don't have a cb arg, then it must have been called with the
// shorter form that doesn't have formData
if (!cb) {
cb = formDataArg;
formData = null;
}
// continue rest of function using cb and formData as the symbols
// for the last two arguments
// ... other code here ...
}

正如其他人所说,当你得到这么多参数时,通常最好使用一个附加属性的选项对象。然后,使一些参数成为可选参数变得容易多了。

你可能也会发现这很有用:

如何在javascript中重载函数?


FYI,还有一种方法可以用更难类型的语言解决类似问题。您可以创建第二个函数,该函数接受额外的参数并将实现移到那里。然后,原始函数就变成了一个shell,它用null为新参数调用新函数。

function sendMessage2(type,url,method,json,retries,formDataArg,cbArg) {
// full implementation here with all arguments, formDataArg may be null
}
// original function here, calling signature unchanged
function sendMessage(type,url,method,json,retries,cbArg) {
// call new implementation with arguments in the right place
return sendMessage2(type, url, method, json, retries, null, cbArg);
}

虽然这是有效的,并且不使用重载,但这是一种一次性的尝试,因为你不想最终使用sendMesage3、sendMessage4等。也许sendMessage2应该使用可扩展性更强的options对象,这样你就不会再被迫这样做了。然后,稍后当您具有更大的灵活性时,您可以将较旧的代码切换到选项对象,并完全取消整个两个API方案。

可能是这样的:

function sendMessageOptions(options, cb) {
// main code here that supports formData to send the message
// gets arguments from the options object
}
// original function here, calling signature unchanged
function sendMessage(type,url,method,json,retries,cbArg) {
// call new implementation with arguments in the right place
return sendMessageOptions({type: type, url: url: method: method, json: json: retries: retries});
}

注意,我将回调排除在选项对象之外,因为这使函数与典型的异步调用约定兼容,并使调用方的内联回调更易于编码。

创建一个接受对象作为参数的新方法,该方法分析对象并决定是代理到原始函数还是执行新需求。

将原始方法标记为不推荐使用,并记录以使用新方法,当时机成熟时,可以替换对原始方法的调用。当它们都被替换后,重构新函数以删除代理,如果确信,则删除旧方法。

尝试从原始方法中提取尽可能多的功能,以保持代码干燥。

如果可能的话,写一个原始和新方法都能通过的测试

最新更新