(function() {
var a = {
createNode: function(d, c, f) {
var b = f || document;
var e = b.createElement(d);
if (c) {
a.each(c,
function(g, h) {
if (g === "class") {
e.className = h
} else {
e.setAttribute(g, h)
}
})
}
return e
},
isDOM: function(b) {
return b !== null && b.nodeType === 1
},
isArray: function(b) {
return Object.prototype.toString.call(b) === "[object Array]"
},
isFunction: function(b) {
return Object.prototype.toString.call(b) === "[object Function]"
},
browser: function() {
var b = {};
var c = navigator.userAgent.toLowerCase();
var d = null;
if (d = c.match(/chrome\/([\d.]+)/)) {
b.chrome = d[1]
} else {
if (d = c.match(/firefox\/([\d.]+)/)) {
b.firefox = d[1]
} else {
if (d = c.match(/msie ([\d.]+)/)) {
b.msie = d[1]
}
}
}
return b
} (),
each: function(d, g, f) {
if (d === null) {
return
}
if (d.length === undefined || a.isFunction(d)) {
for (var c in d) {
if (d.hasOwnProperty(c)) {
if (g.call(f || d[c], c, d[c]) === false) {
break
}
}
}
} else {
for (var e = 0,
b = d.length; e < b; e++) {
if (g.call(f || d[e], e, d[e]) === false) {
break
}
}
}
return d
},
indexOf: function(c, d) {
if (c.indexOf) {
return c.indexOf(d)
} else {
var b = -1;
a.each(c,
function(e) {
if (this === d) {
b = e;
return false
}
});
return b
}
},
log: function() {
if (window.console !== undefined && window.console.log !== undefined) {
var d = a.makeArray(arguments);
d.unshift("[YoudaoUtils]");
try {
window.console.log.apply(window.console, d)
} catch(g) {
var b = "[YoudaoUtils.log]";
for (var f = 0,
c = arguments.length; f < c; f++) {
b += (" " + arguments[f])
}
window.console.log(b)
}
}
},
makeArray: function(b) {
return Array.prototype.slice.call(b, 0)
},
prototypeExtend: function(b, c) {
var d = a.isFunction(c) ? c: function() {};
d.prototype = b;
return new d()
},
location: function() {
return !! window.location ? window.location: !!document.location ? document.location: null
},
url: function() {
var b = a.location();
if ( !! b && b.href !== undefined) {
return b.href
} else {
return null
}
},
bind: function(c, b, d) {
if (!d) {
return
}
if (c.addEventListener) {
c.addEventListener(b, d, false)
} else {
if (c.attachEvent) {
c.attachEvent("on" + b, d)
} else {
c["on" + b] = d
}
}
return this
},
unbind: function(c, b, d) {
if (!d) {
return
}
if (c.removeEventListener) {
c.removeEventListener(b, d, false)
} else {
if (c.detachEvent) {
c.detachEvent("on" + b, d)
} else {
c["on" + b] = function() {}
}
}
return this
},
stopPropagation: function(c) {
var b = c || window.event;
if (b.stopPropagation) {
b.stopPropagation()
} else {
b.cancelBubble = true
}
return b
},
preventDefault: function(c) {
var b = c || window.event;
if (b.preventDefault) {
b.preventDefault()
} else {
b.returnValue = false
}
return b
},
getSelectionText: function() {
var b = null;
if (window.getSelection) {
b = window.getSelection().toString()
}
if ( !! b) {
return b
} else {
if (document.selection) {
return document.selection.createRange().text
} else {
return null
}
}
},
trim: function(b) {
return b.replace(/^\s*/, "").replace(/\s*$/, "")
},
parameter: function(c) {
var b = [];
a.each(c,
function(d, e) {
b.push(encodeURIComponent(d) + "=" + encodeURIComponent(e))
});
return b.join("&")
},
formatTemplate: function(e, f) {
var c = document.createElement("div");
for (var d in f) {
if (f.hasOwnProperty(d)) {
e = e.replace(new RegExp("{" + d + "}", "g"), f[d])
}
}
c.innerHTML = e;
var b = c.firstChild;
c.removeChild(b);
return b
},

解决方案 »

  1.   

    getDocumentCharset: function() {
    return document.characterSet || document.charset
    },
    css: function() {
    var b = function(g, c) {
    var h = "";
    if (c == "float") {
    document.defaultView ? c = "float": c = "styleFloat"
    }
    if (g.style[c]) {
    h = g.style[c]
    } else {
    if (g.currentStyle) {
    h = g.currentStyle[c]
    } else {
    if (document.defaultView && document.defaultView.getComputedStyle) {
    c = c.replace(/([A-Z])/g, "-$1").toLowerCase();
    var d = document.defaultView.getComputedStyle(g, "");
    h = d && d.getPropertyValue(c)
    } else {
    h = null
    }
    }
    }
    if ((h == "auto" || h.indexOf("%") !== -1) && ("width" === c.toLowerCase() || "height" === c.toLowerCase()) && g.style.display != "none" && h.indexOf("%") !== -1) {
    h = g["offset" + c.charAt(0).toUpperCase() + c.substring(1).toLowerCase()] + "px"
    }
    if (c == "opacity") {
    try {
    h = g.filters["DXImageTransform.Microsoft.Alpha"].opacity;
    h = h / 100
    } catch(i) {
    try {
    h = g.filters("alpha").opacity
    } catch(f) {}
    }
    }
    return h
    };
    return function(d, c) {
    if (typeof c === "string") {
    return b(d, c)
    } else {
    a.each(c,
    function(e, f) {
    d.style[e] = f
    })
    }
    }
    } (),
    hasClass: function(f, e) {
    if (a.isDOM(f)) {
    if (f.className === e) {
    return true
    }
    var d = f.className.split(" ");
    for (var c = 0,
    b = d.length; c < b; c++) {
    if (e === d[c]) {
    return true
    }
    }
    }
    return false
    },
    loadCSS: function(f, c) {
    if (f && f.createElement) {
    var e = f.createElement("link");
    var b = a.generateResourceLink(c);
    e.setAttribute("rel", "stylesheet");
    e.setAttribute("href", b);
    e.setAttribute("type", "text/css");
    var d = f.getElementsByTagName("head")[0] || f.body;
    d.appendChild(e)
    }
    },
    loadCSSToLink: function(e, f, c) {
    if (e) {
    var d = e.getElementById(f);
    if (d) {
    var b = a.generateResourceLink(c);
    d.setAttribute("href", b)
    }
    }
    },
    generateResourceLink: function(b) {
    var c = null;
    if ("349344" === "revision") {
    c = (new Date()).getTime()
    } else {
    c = "349344"
    }
    return b.indexOf("?") === -1 ? b + "?" + c: b + "&" + c
    },
    addClass: function(f, e) {
    if (a.isDOM(f)) {
    var d = f.className.split(" ");
    for (var c = 0,
    b = d.length; c < b; c++) {
    if (e === d[c]) {
    return
    }
    }
    d.push(e);
    f.className = d.join(" ")
    }
    },
    removeClass: function(f, e) {
    if (a.isDOM(f)) {
    var d = f.className.split(" "),
    g = [];
    for (var c = 0,
    b = d.length; c < b; c++) {
    if (e !== d[c]) {
    g.push(d[c])
    }
    }
    f.className = g.join(" ")
    }
    },
    toggleClass: function(f, e) {
    if (a.isDOM(f)) {
    var d = f.className.split(" "),
    h = [],
    g = "add";
    for (var c = 0,
    b = d.length; c < b; c++) {
    if (e === d[c]) {
    g = "remove"
    } else {
    h.push(d[c])
    }
    }
    if (g === "add") {
    d.push(e)
    } else {
    d = h
    }
    f.className = d.join(" ")
    }
    },
    scroll: function() {
    return {
    left: document.body.scrollLeft || document.documentElement.scrollLeft,
    top: document.body.scrollTop || document.documentElement.scrollTop
    }
    },
    windowSize: function() {
    return {
    width: document.documentElement.clientWidth,
    height: document.documentElement.clientHeight
    }
    },
    storage: function(c, e) {
    var d = function(g, h) {
    var f = window.localStorage;
    if (h === undefined) {
    return f.getItem(g)
    }
    if (g !== undefined && h !== undefined) {
    f.setItem(g, h);
    return h
    }
    };
    var b = function(g, h) {
    var f = document.documentElement;
    f.addBehavior("#default#userData");
    if (h === undefined) {
    f.load("youdao");
    return f.getAttribute(g)
    }
    if (g !== undefined && h !== undefined) {
    f.setAttribute(g, h);
    f.save("youdao");
    return h
    }
    };
    if ( !! window.localStorage) {
    return d(c, e)
    }
    if ( !! document.documentElement.addBehavior) {
    return b(c, e)
    }
    },
    cookie: function(b, e) {
    function c(g, h) {
    var i = 30;
    var f = new Date();
    f.setTime(f.getTime() + i * 24 * 60 * 60 * 1000);
    document.cookie = g + "=" + encodeURIComponent(h) + ";expires=" + f.toGMTString()
    }
    function d(g) {
    var f = document.cookie.match(new RegExp("(^| )" + g + "=([^;]*)(;|$)"));
    if (f != null) {
    return decodeURIComponent(f[2])
    } else {
    return null
    }
    }
    if ( !! e) {
    c(b, e)
    } else {
    return d(b)
    }
    },
    parseData: function() {
    var b = {
    json: function(c) {
    try {
    return c = JSONYoudao.parse(c)
    } catch(d) {
    a.log("[Error]", "Invalid JSON data:", c)
    }
    },
    xml: function(d) {
    if (window.DOMParser) {
    return (new DOMParser()).parseFromString(d, "text/xml")
    } else {
    var c = new ActiveXObject("Microsoft.XMLDOM");
    c.async = "false";
    c.loadXML(d);
    return c
    }
    }
    };
    return function(c, d) {
    if (a.isFunction(c)) {
    return c(d)
    }
    if (typeof c !== "string") {
    a.log("[Error]", "Function parseData() encounters invalid type");
    return d
    } else {
    if (!b[c]) {
    a.log("[Error]", "Function parseData() dosen't support this type:", c);
    return d
    } else {
    return b[c](d)
    }
    }
    }
    } (),
    guid: function() {
    var b = function() {
    var e = (1 + Math.random()) * 65536;
    var d = e | 0;
    var c = d.toString(16).substring(1);
    return c
    };
    return (b() + b() + "-" + b() + "-" + b() + "-" + b() + "-" + b() + b() + b())
    } ()
    };
    window.YoudaoUtils = a
    })(); 
    疑问:1.(function(){})()这种写法什么意思?
          2.a是一个js 类吗?
          3.怎么样使用a?
      

  2.   

    1. (function(){})() 这是一种匿名函数function(){},并且执行它()
    2. a是个object,一个JSON格式的object
    3. 简单来说 object[propertyName]使用就行了,或者object.propertyName
      

  3.   

    为什么要使用匿名函数?直接写一个js 类 调用 不是一样的吗?有啥好处?写成这样 (function(){//do something })() 一打开浏览器就会执行的 
      

  4.   

    写成这样 (function(){//do something })()可以防止变量冲突<script type="text/javascript">var a = 2;(function(){
    var a = 1; alert(a);
    })()alert(a)
    </script>