!!window.__defineGetter__ && !/MSIE/.test(navigator.userAgent) && function () {
!window.opera && window.__defineGetter__('event', function () {
//兼容Event对象
var o = arguments.callee;
do {
if (o.arguments[0] instanceof Event) return o.arguments[0];
} while (o = o.caller);
return null;
}); window.attachEvent = Document.prototype.attachEvent = Element.prototype.attachEvent = function (type, listener, capture) {
//兼容attachEvent方法
return this.addEventListener(new String(type).substr(2), listener, capture || false);
}; window.detachEvent = Document.prototype.detachEvent = Element.prototype.detachEvent = function (type, listener, capture) {
//兼容detachEvent方法
return this.removeEventListener(new String(type).substr(2), listener, capture || false);
}; }();
这代码还真看不懂,,,高手祥细解释一下。。祥细祥细祥细祥细祥细祥细祥细祥细祥细祥细祥细祥细祥细祥细祥细祥细祥细祥细
function () {
!window.opera && window.__defineGetter__('event', function () {
//兼容Event对象
var o = arguments.callee;
do {
if (o.arguments[0] instanceof Event) return o.arguments[0];
} while (o = o.caller);
return null;
}); window.attachEvent = Document.prototype.attachEvent = Element.prototype.attachEvent = function (type, listener, capture) {
//兼容attachEvent方法
return this.addEventListener(new String(type).substr(2), listener, capture || false);
}; window.detachEvent = Document.prototype.detachEvent = Element.prototype.detachEvent = function (type, listener, capture) {
//兼容detachEvent方法
return this.removeEventListener(new String(type).substr(2), listener, capture || false);
};}();
!! window.__defineGetter__ 把 window.__defineGetter__ 转换成bool,是否支持__defineGetter__ 对象
&& 逻辑与运算符,如果第一个条件成立判断第二个
!/MSIE/.test(navigator.userAgent)判断 非IE浏览器然后声明并执行一个匿名函数!利用运算符作条件判断,比用 if(){} 高效
这都用上了?
我可从来不敢在顶层对象上做手脚啊
.....
}
不就是一个赋值运算符吗可以看作:
var v1=v2=v3={};==》
v3={};
v2=v3;
v1=v2;==>
v1={};
v2={};
v3={};
而:var o = arguments.callee;//正在执行的调用者本身
do {
if (o.arguments[0] instanceof Event) return o.arguments[0];
} while (o = o.caller);//当调用者存在的时候,不存在就为FALSE,循环结束,返回空值!
return null;
});
这里应该就是核心,让2个事件兼容IE跟FF的。
可能我这么理解也有错误,欢迎大师更正!
!!window.__defineGetter__ && !/MSIE/.test(navigator.userAgent) && function (){}();
这是一个条件判断,但也起到了if的作用,利用了逻辑运算的短路,相当于一个隐藏的if条件
关于逻辑运算短路自己去搜一下百度或google最后一部分function (){}();中间的代码,在前面的条件成立的时候,会被触发运行
而其中的!window.opera && window.__defineGetter__() 又是一个逻辑短路window.__defineGetter__()中间的内容,定义了事件兼容代码
.....
}
是什么意思, 并不是赋不赋值的问题,,,, 我的意思是 window.attachEvent 是IE 下window对象的attachEvent 是用绑定事件,, Document.prototype.attachEvent 这个是什么意思,,
Document是如果是一个对象 的话 ,但它又没有定义这个对象,并不是 document对象。。Document不是一个对象 ,怎么又一个对象原型 prototype 而对象原型 怎么又有一个 attachEvent方法呢? 请大家祥细一点吧。。