读jQuery之九 一些瑕疵说明
// Handle object literals
if ( typeof type === "object" ) {
for ( var key in type ) {
this[ name ](key, data, type[key], fn);
}
return this;
}
// Add which for click: 1 === left; 2 === middle; 3 === right
// Add which for mousedown/mouseup : 1 === left; 2 === middle; 3 === right
3,data方法,定义了局部变量internalKey,但后续代码仍然沿用jQuery.expando。
代码如下:
var internalKey = jQuery.expando, getByName = typeof name === "string", thisCache,
4,jQuery.event.add方法,arguments改为e,apply改为call 更好一些。因为只会传一个参数:事件对象。
代码如下:
elemData.handle = eventHandle = function( e ) {
// Discard the second event of a jQuery.event.trigger() and
// when an event is called after a page has unloaded
return typeof jQuery !== "undefined" && (!e || jQuery.event.triggered !== e.type) ?
jQuery.event.handle.apply( eventHandle.elem, arguments ) :
undefined;
};
5,基于 “勿重复检测浏览器” 原则,jQuery.event.add 以下代码
代码如下:
if ( elem.addEventListener ) {
elem.addEventListener( type, eventHandle, false );
} else if ( elem.attachEvent ) {
elem.attachEvent( "on" + type, eventHandle );
}
jQuery.addEvent = document.addEventListener ?
function( elem, type, handle ) {
if ( elem.addEventListener ) {
elem.addEventListener( type, handle, false );
}
} :
function( elem, type, handle ) {
if ( elem.attactEvent ) {
elem.attactEvent( "on" + type, handle );
}
};
事实上,jQuery中已经有了jQuery.removeEvent,却不知为何没有jQuery.addEvent。