惰性载入函数

//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;
        }

 

posted @ 2017-09-07 09:28  longdb  阅读(184)  评论(0编辑  收藏  举报