1,bind 方法,最後一個參數fn是多余的
代碼如下:
// Handle object literals
if ( typeof type === "object" ) {
for ( var key in type ) {
this[ name ](key, data, type[key], fn);
}
return this;
}
2,注釋
代碼如下:
// 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。