[英]How to combine similar functions - Javascript
I have two functions that are very similar and I would like if possible to combine them. 我有两个非常相似的功能,如果可能的话,我希望将它们结合起来。 The only issue that I have is one function is accepting 2 arguments and the other one is accepting 3. Is there a way to do this or these two functions have to stay separated by design?
我唯一的问题是一个函数正在接受2个参数,另一个函数正在接受3。是否有办法做到这一点,或者这两个函数必须按设计分开?
function getClientData(id, command) {
var formData = {
'method': command,
'id': id
};
getEntityData(formData);
}
function getLocation(id, clientid, command) {
var formData = {
'method': command,
'locationid': id,
'clientbrandid': clientid
};
getEntityData(formData);
}
Update 更新资料
function getEntityData(data) {
var url = '/../admin/miscellaneous/components/global.cfc?wsdl';
var ajaxResponse = $.ajax({
url: url,
dataType: 'json',
data: data,
global: false,
async:false,
cache: false,
success: function(apiResponse){
return apiResponse;
}
}).responseJSON;
var response = ajaxResponse[0];
for (var i in response) {
if (response.hasOwnProperty(i)){
$("#edit"+i).val(response[i].trim());
}
}
}
yes you can, I prefer instead of passing each parameter you can pass a js object, and decide wich params it contains for example: 是的,可以,我更喜欢代替传递每个参数,而可以传递一个js对象,并确定它所包含的参数,例如:
function getLocation(options) {
getEntityData(options);
}
and your call should be: 您的电话应该是:
getLocation({'method': command,'id': id})
Update 更新资料
or you can just avoid getLocation function and just call getEntityData 或者您可以避免使用getLocation函数,而只需调用getEntityData
getEntityData({
'method': command,
'id': id
});
function getLocation(id, clientid, command) {
var formData = {
'method': command,
'locationid': id
};
if (clientid) {
formData['clientbrandid'] = clientid;
}
getEntityData(formData);
}
// With
getLocation(1, 2, 'doStuff');
// Without
getLocation(1, '', 'doStuff');
Maybe a more rational order of arguments: 也许是一个更合理的论证顺序:
function getLocation(id, command, clientid) {
var formData = {
'method': command,
'locationid': id
};
if (clientid) {
formData['clientbrandid'] = clientid;
}
getEntityData(formData);
}
// With
getLocation(1, 'doStuff', 2);
// Without
getLocation(1, 'doStuff');
And if locationid
and id
are different: 如果
locationid
和id
不同:
function getLocation(id, command, clientid) {
if (clientid) {
var formData = {
'method': command,
'locationid': id,
'clientbrandid': clientid
};
} else {
var formData = {
'method': command,
'id': id,
};
}
getEntityData(formData);
}
// With
getLocation(1, 'doStuff', 2);
// Without
getLocation(1, 'doStuff');
I guess it really depends on what your arguments actually are, but this is also a solution. 我想这真的取决于您的论点到底是什么,但这也是一个解决方案。 (Assuming that client id is an object).
(假设客户端id是一个对象)。
function getLocation(id, command, clientid) {
var _clientId = clientid || {};
var formData = {
'method': command,
'locationid': id,
'clientbrandid': _clientid
};
getEntityData(formData);
}
I would go with: 我会去:
function getWhatever(id, command, clientId) {
var formData = { method: command };
if (typeof clientId === 'undefined') {
formData.id = id;
} else {
formData.locationid = id;
formData.clientbrandid = clientId;
}
getEntityData(formData);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.