How to combine similar functions - Javascript - Stack Overflow

I have two functions that are very similar and I would like if possible to bine them. The only issue th

I have two functions that are very similar and I would like if possible to bine 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?

function getClientData(id, mand) {

    var formData = {
        'method': mand,
        'id': id
    };

    getEntityData(formData);
}


function getLocation(id, clientid, mand) {

    var formData = {
        'method': mand,
        'locationid': id,
        'clientbrandid': clientid
    };

    getEntityData(formData);
}

Update

function getEntityData(data) {

    var url = '/../admin/miscellaneous/ponents/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());
        }
    }
}

I have two functions that are very similar and I would like if possible to bine 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?

function getClientData(id, mand) {

    var formData = {
        'method': mand,
        'id': id
    };

    getEntityData(formData);
}


function getLocation(id, clientid, mand) {

    var formData = {
        'method': mand,
        'locationid': id,
        'clientbrandid': clientid
    };

    getEntityData(formData);
}

Update

function getEntityData(data) {

    var url = '/../admin/miscellaneous/ponents/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());
        }
    }
}
Share Improve this question edited Apr 24, 2014 at 22:32 Geo asked Apr 24, 2014 at 22:15 GeoGeo 3,2007 gold badges44 silver badges87 bronze badges 2
  • Really you have 4 parameters as id and locationid are passed to getEntityData with different names. Are they the same thing? You haven't shown what getEntityData does. – RobG Commented Apr 24, 2014 at 22:26
  • I will update now. The getEntityData is just an ajax function which takes the data in the data: section. – Geo Commented Apr 24, 2014 at 22:31
Add a ment  | 

4 Answers 4

Reset to default 4

yes you can, I prefer instead of passing each parameter you can pass a js object, and decide wich params it contains for example:

function getLocation(options) {

    getEntityData(options);
}

and your call should be:

getLocation({'method': mand,'id': id})

Update

or you can just avoid getLocation function and just call getEntityData

getEntityData({
    'method': mand,
    'id': id
});

I would go with:

function getWhatever(id, mand, clientId) {
    var formData = { method: mand };

    if (typeof clientId === 'undefined') {
        formData.id = id;
    } else {
        formData.locationid = id;
        formData.clientbrandid = clientId;
    }

    getEntityData(formData);
}
function getLocation(id, clientid, mand) {
    var formData = {
        'method': mand,
        '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, mand, clientid) {
    var formData = {
        'method': mand,
        '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:

function getLocation(id, mand, clientid) {
    if (clientid) {
        var formData = {
            'method': mand,
            'locationid': id,
            'clientbrandid': clientid
        };
    } else {
        var formData = {
            'method': mand,
            '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).

function getLocation(id, mand, clientid) {

    var _clientId = clientid || {};

    var formData = {
        'method': mand,
        'locationid': id,
        'clientbrandid': _clientid
    };

    getEntityData(formData);
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745483147a4629649.html

相关推荐

  • How to combine similar functions - Javascript - Stack Overflow

    I have two functions that are very similar and I would like if possible to bine them. The only issue th

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信