3
\$\begingroup\$

I would like some eyes on this:

/* Little XHR
 * by: rlemon        http://github.com/rlemon/
 * see README for useage.
 * */
var xhr = {
    xmlhttp: (function() {
        var xmlhttp;
        try {
            xmlhttp = new XMLHttpRequest();
        } catch (e) {
            try {
                xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
            } catch (er) {
                try {
                    xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
                } catch (err) {
                    xmlhttp = false;
                }
            }
        }
        return xmlhttp;
    }()),
     /* https://github.com/Titani/SO-ChatBot/blob/ccf6cfe827aee2af7b2832e48720a8e24a8feeed/source/bot.js#L110 */
    urlstringify: (function() {
        var simplies = {
            'number': true,
            'string': true,
            'boolean': true
        };
        var singularStringify = function(thing) {
            if (typeof thing in simplies) {
                return encodeURIComponent(thing.toString());
            }
            return '';
        };
        var arrayStringify = function(array, keyName) {
            keyName = singularStringify(keyName);
            return array.map(function(thing) {
                return keyName + '=' + singularStringify(thing);
            });
        };
        return function(obj) {
            return Object.keys(obj).map(function(key) {
                var val = obj[key];
                if (Array.isArray(val)) {
                    return arrayStringify(val, key);
                } else {
                    return singularStringify(key) + '=' + singularStringify(val);
                }
            }).join('&');
        };
    }()),
    post: function(options) {
        this.request.apply(this, ['POST', options]);
    },
    get: function(options) {
        this.request.apply(this, ['GET', options]);
    },
    request: function(type, options) {
        if (this.xmlhttp && options && 'url' in options) {
            var xhr = this.xmlhttp,
                enctype = ('enctype' in options) ? options.enctype : 'application/x-www-form-urlencoded';
            xhr.open(type, options.url, true);
            xhr.setRequestHeader('Content-Type', enctype);
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4) {
                    if (xhr.status == 200) {
                        if ('success' in options && options.success.apply) {
                            options.success.apply(this, [xhr]);
                        }
                    } else if (xhr.status && xhr.status != 200) {
                        if ('failure' in options && options.failure.apply) {
                            options.failure.apply(this, [xhr]);
                        }
                    }
                }
            };
            var data = null;
            if ('data' in options) {
                data = this.urlstringify.apply(this, [options.data]);
            }
            xhr.send(data);
        }
    }
};

For a barebones object; have I missed anything important? Should I change how any of this is organized or layed out? I have also tested in a limited number of browsers and so far so good! However I am unsure about older browsers and compatibility.

Any input would be great! thanks!

\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

Its interface might not be obvious enough for junior programmers in a hurry. I suppose there's some assistance in your README. Anyway the code is quite clear given a little experience or effort. It doesn't use exactly the subset of JavaScript that Douglas Crockford recommended in JavaScript: The Good Parts, but that doesn't always matter.

I like this code. It has enough complexity for the real world without taking on responsibilities extraneous to wrapping XmlHttpRequest (or its equivalents). Nice use of closures. I really like that you incorporated a hyperlink to a line of github code that you found helpful.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.