I have a question that may seem trivial, but I want to make sure I'm heading in the right direction.
I've created two different versions of an XMLHttpRequest wrapper, and both are functioning correctly.
const httpRequest = function () {
let xhr = new XMLHttpRequest();
return function ( config = { url: '', method: 'GET', onLoadStart: null, onLoad: null, onProgress: null, onError: null, onAbort: null, onLoadEnd: null, onTimeout: null, timeout : null }) {
xhr.open(config.method, config.url);
xhr.onloadstart = config.onLoadStart;
xhr.onload = config.onLoad;
xhr.onprogress = config.onProgress;
xhr.onerror = config.onError;
xhr.onabort = config.onAbort;
xhr.onloadend = config.onLoadEnd;
xhr.ontimeout = config.onTimeout;
xhr.timeout = config.timeout;
return xhr;
};
}();
and this one
function httpRequest(config = { url: '', method: 'GET', onLoadStart: null, onLoad: null, onProgress: null, onError: null, onAbort: null, onLoadEnd: null, onTimeout: null, timeout : null }) {
var xhr = new XMLHttpRequest();
xhr.open(config.method, config.url);
xhr.onloadstart = config.onLoadStart;
xhr.onload = config.onLoad;
xhr.onprogress = config.onProgress;
xhr.onerror = config.onError;
xhr.onabort = config.onAbort;
xhr.onloadend = config.onLoadEnd;
xhr.ontimeout = config.onTimeout;
xhr.timeout = config.timeout;
return xhr;
}
I'm having trouble understanding why you would use an immediately-invoked Function Expression when a regular function works just as well? I'm hoping someone can provide some insight, as I've heard that for this type of scenario, an immediately invoked function is preferred.