Home  >  Article  >  Web Front-end  >  Read jQuery Part 9: Some Defects Explained_jquery

Read jQuery Part 9: Some Defects Explained_jquery

WBOY
WBOYOriginal
2016-05-16 18:05:501047browse

1. The bind method, the last parameter fn is redundant

Copy the code The code is as follows:

// Handle object literals
if ( typeof type === "object" ) {
for ( var key in type ) {
this[ name ](key, data, type[key], fn) ;
}
return this;
}

2, comment
Copy code The code is as follows:

// Add which for click: 1 === left; 2 === middle; 3 === right

should be modified Copy the code for
The code is as follows:

// Add which for mousedown/mouseup : 1 = == left; 2 === middle; 3 === right

3, data method, defines the local variable internalKey, but subsequent code still uses jQuery.expando.
Copy code The code is as follows:

var internalKey = jQuery.expando, getByName = typeof name = == "string", thisCache,

4, jQuery.event.add method, arguments are changed to e, and apply is changed to call, which is better. Because only one parameter is passed: the event object.
Copy code The code is as follows:

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, based on the principle of "do not detect browsers repeatedly", jQuery.event.add the following code
Copy code code As follows:

if ( elem.addEventListener ) {
elem.addEventListener( type, eventHandle, false );

} else if ( elem.attachEvent ) {
elem.attachEvent( "on" type, eventHandle );
}

should be replaced with the following:
Copy code The code is as follows:

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

In fact, jQuery already has jQuery.removeEvent, but for some reason there is no jQuery.addEvent.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn