惰性载入函数

//createXHR当作变量,这样能消除if分支多次判断.    
function createXHR()
        {
            if (typeof XMLHttpRequest != "undefined") {
                createXHR = function () {
                    return new XMLHttpRequest();//最重要的对象.
                };
            }
            else if (typeof ActiveXObject != "undefined")
            {
                createXHR = function () {
                    try {
                        return new ActiveXObject("Msxml2.XMLHTTP");
                    } catch (othermicrosoft) {
                        try {
                            return new ActiveXObject("Microsoft.XMLHTTP");
                        } catch (failed) {

                        }
                    }
                };
                return createXHR;
        }
原文地址:https://www.cnblogs.com/longdb/p/7488110.html