How does the jQuery .unbind() function work in JavaScript? -
some background: javascript function .removeeventlistener() requires explicit declaration of handler want remove, while jquery's .unbind() automatically remove event listeners associated given element.
taking @ jquery source code, i'm pretty confused how .unbind() function doing it's doing. how removing possible handlers associated given element, if doesn't know them name?
edit: sorry if wasn't clear. question "how javascript source code .unbind() work?" know how use .unbind(). want know why works.
if see jquery-1.11.js find answer
jquery.removeevent called lastly, when call unbind()
jquery.removeevent = document.removeeventlistener ? function( elem, type, handle ) { if ( elem.removeeventlistener ) { elem.removeeventlistener( type, handle, false ); } } : function( elem, type, handle ) { var name = "on" + type; if ( elem.detachevent ) { // #8545, #7054, preventing memory leaks custom events in ie6-8 // detachevent needed property on element, name of event, expose gc if ( typeof elem[ name ] === strundefined ) { elem[ name ] = null; } elem.detachevent( name, handle ); } }; for egs try unbind(),
$('#selector').unbind('click',handler); above has elem,type, , handle in removeeventlistener() : function( elem, type, handle )
here
- elem=$('#selector')
- type=click
- handle=handler
step step call of unbind() see jquery-1.11.1.js source
$('#selector').unbind('click') // calling unbind function line 8490- which calls off()
// line 8491(calling) => line 5201(function definition) - now calling
event.remove()// line 5229 => line 4374 - which calls
removedata()removeeventlistenerfunction called
Comments
Post a Comment