0

I am trying to write my own Xhr wrapper as practice. I am in the process of getting things to work cross browser I came across this piece of code that initiates a xhr object for all browsers IE5.5+

But i personally dont really understand how to now use this instantiated xhr object.

See the code below:

(function () {
    try {
        return new(this.XMLHttpRequest || ActiveXObject)('MSXML2.XMLHTTP.3.0');
    } catch (e) {}
})();

Could someone explain to me how to use the instantiated xhr object?
And maybe for my own learning experience how this (function (){})(); thing works

If something is unclear let me know so I can clarify!

1 Answer 1

2

Your expression is an IIFE - you define a function and call it right away. Since you don't assign the result to anything, it's lost. So either do

var req = (function () {
    try {
        return new(this.XMLHttpRequest || ActiveXObject)('MSXML2.XMLHTTP.3.0');
    } catch (e) {}
})();

or (more readable)

function getRequest() {
    try {
        return new(this.XMLHttpRequest || ActiveXObject)('MSXML2.XMLHTTP.3.0');
    } catch (e) {}
}

var req = getRequest()

Not sure about all that ActiveXObject stuff though.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.