简体   繁体   中英

Function that receives a parameter and returns it with no modifications

I can not figure out what could be the usefulness for this function:

// Quick function to retrieve the parameters in a format compatible with the ajax request
var getRequestParameters = function(params){
    return params;
};

I'm seeing it in a web project, used before every $.ajax() call. Can anybody please enlighten me?

It's an extension point. It serves as a central point to add/remove/customize parameters at some point in the future if the needs of the project change. At present, it doesn't do anything, but apparently it's there on the theory that eventually it may do something.

You've said it's used before every $.ajax call, which suggests you're using jQuery. Which means it's a bit redundant with jQuery's own ajaxSend , but that's not a big deal.

For example, suppose at some point in the future you needed to add a unique identifier to every ajax request that was sent. You'd just have to modify that one function:

var id = 0;
var getRequestParameters = function(params){
    params.__uniqueId = ++id; // Or perhaps copy params first
    return params;
};

...and you'd have it on all of your ajax requests.

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