<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Shorthand methods for unbind</title>
	<atom:link href="http://www.learningjquery.com/2009/06/shorthand-methods-for-unbind/feed" rel="self" type="application/rss+xml" />
	<link>http://www.learningjquery.com/2009/06/shorthand-methods-for-unbind</link>
	<description>Tips, techniques, and tutorials for the jQuery JavaScript library</description>
	<lastBuildDate>Wed, 08 Feb 2012 13:50:43 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Shorthand methods for unbind in jQuery &#171; Steele</title>
		<link>http://www.learningjquery.com/2009/06/shorthand-methods-for-unbind/comment-page-1#comment-79129</link>
		<dc:creator>Shorthand methods for unbind in jQuery &#171; Steele</dc:creator>
		<pubDate>Wed, 28 Oct 2009 18:01:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.learningjquery.com/?p=798#comment-79129</guid>
		<description>[...] or &quot;one&quot; method to the jQuery prototype (jQuery.fn). Here is what it might look like:  PLAIN TEXT  [...]</description>
		<content:encoded><![CDATA[<p>[...] or &quot;one&quot; method to the jQuery prototype (jQuery.fn). Here is what it might look like:  PLAIN TEXT  [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Karl Swedberg</title>
		<link>http://www.learningjquery.com/2009/06/shorthand-methods-for-unbind/comment-page-1#comment-78474</link>
		<dc:creator>Karl Swedberg</dc:creator>
		<pubDate>Tue, 01 Sep 2009 19:30:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.learningjquery.com/?p=798#comment-78474</guid>
		<description>That looks really promising. you should post it to the &lt;a href=&quot;http://groups.google.com/group/jquery-dev/&quot; rel=&quot;nofollow&quot;&gt;jquery-dev list&lt;/a&gt;.</description>
		<content:encoded><![CDATA[<p>That looks really promising. you should post it to the <a href="http://groups.google.com/group/jquery-dev/" rel="nofollow">jquery-dev list</a>.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Keegan Watkins</title>
		<link>http://www.learningjquery.com/2009/06/shorthand-methods-for-unbind/comment-page-1#comment-78469</link>
		<dc:creator>Keegan Watkins</dc:creator>
		<pubDate>Tue, 01 Sep 2009 14:07:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.learningjquery.com/?p=798#comment-78469</guid>
		<description>Thank you, Karl!

Unfortunately, I&#039;ve changed my mind about this topic... again! (I&#039;m a bit obsessive, it&#039;s fine though). After commenting above, I now feel that this functionality does in fact belong in the core. My initial objections to adding these features to the Core were that:

a.) adding frivolous methods sucks. The jQuery namespace is fairly clean and small, and I like it that way. And, ...

b.) I personally don&#039;t like the idea of iterating through an array to produce dynamic method names in a core library. I use the same technique often, but never in the context of a framework; in my experience it leads to maintenance issues over time (but that&#039;s just a matter of preference, of course :). And finally, ...

c.) adding short-hand methods for all events could grow to be unwieldy, especially when you consider that an &quot;add/bind&quot; and &quot;remove/unbind&quot; method would be needed for each event.

Then after some thought, it dawned on me: What I was really looking for in the comment above was to minimize the number of methods added. The &quot;Holy Grail&quot; of event bindings (for me) would be to be able to create as many (or few)  bindings as I need to on a given DOM collection, and to be able to do so easily (using a single method would be ideal). If only jQuery could determine whether: 

a.) a key/value pair were passed as String/Function arguments, or

b.) a map of type/handler values was passed

This logic is already in place elsewhere in jQuery, for example in the $().css() and $().attr() methods. Both are able to apply individual key and value arguments, and operate once using them. But they&#039;re also capable of applying a map of key/value pairs, iterating over them and applying each pair. So, really, I&#039;m just dreaming of sexier $().bind() and $().unbind() implementations. And so, down the rabbit hole I went...

...and here&#039;s what I came up with:
&lt;code&gt;&lt;pre&gt;
(function($) {
	
// Keep a copy of the old methods
$.fn._bind = $.fn.bind;
$.fn._unbind = $.fn.unbind;
	
// Redefine $().bind()
$.fn.bind = function( type, data, fn ) {
	// If only a map of handlers was passed...
	return (arguments.length === 1) ? 
		
	this.each(function(key, node) {
		// Iterate over the map...
		$.each(type, function(event, handler) {
			event == &quot;unload&quot; ?
				// ... using $.fn.one() for &quot;unload&quot; events...
				$(this).one(event, handler) :
				// ... and $.event.add() for others
				jQuery.event.add( this, event, handler );
		});	
	}) :

	// Otherwise, use the existing implementation as of 1.3.2,
	// with slight syntactic modifications
	this.each(function(key, node) {
		type == &quot;unload&quot; ?
			// Use $.fn.one() for &quot;unload&quot; events...
			$(this).one(type, fn) :
			// ... and $.event.add() for others
			jQuery.event.add( this, type, fn &#124;&#124; data, fn &amp;&amp; data );
	});	
};
	
// Redefine $().unbind()
$.fn.unbind = function(type, fn) {
	// If only a map of handlers was passed...
	return (arguments.length === 1) 

	this.each(function(){
		// Iterate over the map...
		$.each(type, function(event, handler) {
			// ... and unbind each using event.remove()
			jQuery.event.remove( this, event, handler );
		});
	}) :

	// Otherwise, use the existing implementation as of 1.3.2,
	// copied verbatim
	this.each(function(){
		jQuery.event.remove( this, type, fn );
	});
};

})(jQuery);
&lt;/pre&gt;&lt;/code&gt;

Usage would be similar to the existing $().bind() method, but allow for multiple event/handler pairs:
&lt;code&gt;&lt;pre&gt;
$(&quot;#myID&quot;).bind({
	click : function(e) {
		// handle click
	},
	mouseover : function(e) {
		// handle mouseover
	},
	mouseout : function(e) {
		// handle mouseout
	}
});
&lt;/pre&gt;&lt;/code&gt;

Or, with named functions (ideal for later use of $().unbind()):
&lt;code&gt;&lt;pre&gt;
$(&quot;#myID&quot;).bind({
	click : clickHandler,
	mouseover : mouseoverHandler,
	mouseout : mouseoutHandler
});
		
function clickHandler(e) {
	// handle click
}
		
function mouseoverHandler(e) {
	// handle mouseover
}
		
function mouseoutHandler(e) {
	// handle mouseout
}
&lt;/pre&gt;&lt;/code&gt;

With named function references, unbinding multiple events would be a breeze:
&lt;code&gt;&lt;pre&gt;
$(&quot;#myID&quot;).unbind({
	click : clickHandler,
	mouseover : mouseoverHandler,
	mouseout : mouseoutHandler
});
&lt;/pre&gt;&lt;/code&gt;

Now, of course if this were to be absorbed by the Core there would definitely be performance/syntactic/readibility improvements to be made, but it works beautifully as a prototype. The additional check against the number of arguments adds some extra overhead, but for me the ease-of-use justifies the extra processing. Imagine, two simple methods to encapsulate all binding/unbinding behavior... Furthermore, existing scripts which rely on binding events individually would not break, allowing backwards compatibility. All existing logic, such as using $().one() for &quot;unload&quot; events, is still intact. All that has changed is to loop through a map in the case that one is passed. Now, some known issues:

a.) The ability to pass custom data to $().bind() is lost when an object of event/handler pairs is passed. I personally don&#039;t usually use it anyhow, and binding events individually will still support custom data (as the implementation is the same).

b.) Like the solution in my comment above, passing a map of event/handler pairs to $().unbind() requires that the handlers be references to named functions. Passing identical anonymous functions to $().unbind(), (as they were originally passed to $().bind()), will not work, even if the signatures and/or function bodies are the same.

What do you think? Would more versatile implementations of $().bind() and $().unbind() render this entire discussion moot? Thoughts? Are you reading this, John Resig :) ?</description>
		<content:encoded><![CDATA[<p>Thank you, Karl!</p>
<p>Unfortunately, I&#8217;ve changed my mind about this topic&#8230; again! (I&#8217;m a bit obsessive, it&#8217;s fine though). After commenting above, I now feel that this functionality does in fact belong in the core. My initial objections to adding these features to the Core were that:</p>
<p>a.) adding frivolous methods sucks. The jQuery namespace is fairly clean and small, and I like it that way. And, &#8230;</p>
<p>b.) I personally don&#8217;t like the idea of iterating through an array to produce dynamic method names in a core library. I use the same technique often, but never in the context of a framework; in my experience it leads to maintenance issues over time (but that&#8217;s just a matter of preference, of course :). And finally, &#8230;</p>
<p>c.) adding short-hand methods for all events could grow to be unwieldy, especially when you consider that an &#8220;add/bind&#8221; and &#8220;remove/unbind&#8221; method would be needed for each event.</p>
<p>Then after some thought, it dawned on me: What I was really looking for in the comment above was to minimize the number of methods added. The &#8220;Holy Grail&#8221; of event bindings (for me) would be to be able to create as many (or few)  bindings as I need to on a given DOM collection, and to be able to do so easily (using a single method would be ideal). If only jQuery could determine whether: </p>
<p>a.) a key/value pair were passed as String/Function arguments, or</p>
<p>b.) a map of type/handler values was passed</p>
<p>This logic is already in place elsewhere in jQuery, for example in the $().css() and $().attr() methods. Both are able to apply individual key and value arguments, and operate once using them. But they&#8217;re also capable of applying a map of key/value pairs, iterating over them and applying each pair. So, really, I&#8217;m just dreaming of sexier $().bind() and $().unbind() implementations. And so, down the rabbit hole I went&#8230;</p>
<p>&#8230;and here&#8217;s what I came up with:<br />
<code>
<pre>
(function($) {

// Keep a copy of the old methods
$.fn._bind = $.fn.bind;
$.fn._unbind = $.fn.unbind;

// Redefine $().bind()
$.fn.bind = function( type, data, fn ) {
	// If only a map of handlers was passed...
	return (arguments.length === 1) ? 

	this.each(function(key, node) {
		// Iterate over the map...
		$.each(type, function(event, handler) {
			event == "unload" ?
				// ... using $.fn.one() for "unload" events...
				$(this).one(event, handler) :
				// ... and $.event.add() for others
				jQuery.event.add( this, event, handler );
		});
	}) :

	// Otherwise, use the existing implementation as of 1.3.2,
	// with slight syntactic modifications
	this.each(function(key, node) {
		type == "unload" ?
			// Use $.fn.one() for "unload" events...
			$(this).one(type, fn) :
			// ... and $.event.add() for others
			jQuery.event.add( this, type, fn || data, fn &amp;&amp; data );
	});
};

// Redefine $().unbind()
$.fn.unbind = function(type, fn) {
	// If only a map of handlers was passed...
	return (arguments.length === 1) 

	this.each(function(){
		// Iterate over the map...
		$.each(type, function(event, handler) {
			// ... and unbind each using event.remove()
			jQuery.event.remove( this, event, handler );
		});
	}) :

	// Otherwise, use the existing implementation as of 1.3.2,
	// copied verbatim
	this.each(function(){
		jQuery.event.remove( this, type, fn );
	});
};

})(jQuery);
</pre>
<p></code></p>
<p>Usage would be similar to the existing $().bind() method, but allow for multiple event/handler pairs:<br />
<code>
<pre>
$("#myID").bind({
	click : function(e) {
		// handle click
	},
	mouseover : function(e) {
		// handle mouseover
	},
	mouseout : function(e) {
		// handle mouseout
	}
});
</pre>
<p></code></p>
<p>Or, with named functions (ideal for later use of $().unbind()):<br />
<code>
<pre>
$("#myID").bind({
	click : clickHandler,
	mouseover : mouseoverHandler,
	mouseout : mouseoutHandler
});

function clickHandler(e) {
	// handle click
}

function mouseoverHandler(e) {
	// handle mouseover
}

function mouseoutHandler(e) {
	// handle mouseout
}
</pre>
<p></code></p>
<p>With named function references, unbinding multiple events would be a breeze:<br />
<code>
<pre>
$("#myID").unbind({
	click : clickHandler,
	mouseover : mouseoverHandler,
	mouseout : mouseoutHandler
});
</pre>
<p></code></p>
<p>Now, of course if this were to be absorbed by the Core there would definitely be performance/syntactic/readibility improvements to be made, but it works beautifully as a prototype. The additional check against the number of arguments adds some extra overhead, but for me the ease-of-use justifies the extra processing. Imagine, two simple methods to encapsulate all binding/unbinding behavior&#8230; Furthermore, existing scripts which rely on binding events individually would not break, allowing backwards compatibility. All existing logic, such as using $().one() for &#8220;unload&#8221; events, is still intact. All that has changed is to loop through a map in the case that one is passed. Now, some known issues:</p>
<p>a.) The ability to pass custom data to $().bind() is lost when an object of event/handler pairs is passed. I personally don&#8217;t usually use it anyhow, and binding events individually will still support custom data (as the implementation is the same).</p>
<p>b.) Like the solution in my comment above, passing a map of event/handler pairs to $().unbind() requires that the handlers be references to named functions. Passing identical anonymous functions to $().unbind(), (as they were originally passed to $().bind()), will not work, even if the signatures and/or function bodies are the same.</p>
<p>What do you think? Would more versatile implementations of $().bind() and $().unbind() render this entire discussion moot? Thoughts? Are you reading this, John Resig :) ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Karl Swedberg</title>
		<link>http://www.learningjquery.com/2009/06/shorthand-methods-for-unbind/comment-page-1#comment-78452</link>
		<dc:creator>Karl Swedberg</dc:creator>
		<pubDate>Mon, 31 Aug 2009 21:02:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.learningjquery.com/?p=798#comment-78452</guid>
		<description>Thanks, Keegan. That&#039;s a fantastic plugin!</description>
		<content:encoded><![CDATA[<p>Thanks, Keegan. That&#8217;s a fantastic plugin!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Keegan Watkins</title>
		<link>http://www.learningjquery.com/2009/06/shorthand-methods-for-unbind/comment-page-1#comment-78439</link>
		<dc:creator>Keegan Watkins</dc:creator>
		<pubDate>Mon, 31 Aug 2009 13:09:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.learningjquery.com/?p=798#comment-78439</guid>
		<description>Nice work, Karl! I tend to agree that this sort of functionality shouldn&#039;t be in the core, especially considering how easy it is to implement on one&#039;s own. I recently developed a plugin for binding multiple events (both native and custom) in a single call. What I liked about this approach is that a.) only one short-hand method is needed, and b.) the plugin is not coupled to event names at all, allowing future support for new/custom events.

&lt;code&gt;&lt;pre&gt;
$.fn.on = function(eventMap /* Object */) {
    return this.each(function() {
        var elem = $(this);
        // Iterate through the map, binding each event to each handler
        $.each(eventMap, function(type, handler) {
            elem.bind(type, handler);
        });
    });
};
&lt;/pre&gt;&lt;/code&gt;

Usage is as follows:
&lt;code&gt;&lt;pre&gt;
$(&quot;#someID&quot;).on({
    click : function(e) {
        // Handle click 
    },
    mouseover : function(e) {
        // Handle mouseover
    },
    mouseout : function(e) {
        // Handle mouseout
    }
});
&lt;/pre&gt;&lt;/code&gt;

Or, with named function references:
&lt;code&gt;&lt;pre&gt;
$(&quot;#someID&quot;).on({
    click : handleClick,
    mouseover : handleMouseover,
    mouseout : handleMouseout
});

function handleClick(e) {
	// Handle Click
}

function handleMouseover(e) {
	// Handle Mouseover
}

function handleMouseout(e) {
	// Handle Mouseout
}
&lt;/pre&gt;&lt;/code&gt;
Implementing an unbinding method is equally trivial:
&lt;code&gt;&lt;pre&gt;
$.fn.un = function(eventMap /* Object */) {
    return this.each(function() {
        var elem = $(this);
        // Iterate through the map, unbinding each handler
        $.each(eventMap, function(type, handler) {
            elem.unbind(type, handler);
        });
    });
};
&lt;/pre&gt;&lt;/code&gt;

Which enables unbinding in the same manner:
&lt;code&gt;&lt;pre&gt;
$(&quot;#someID&quot;).un({
    click : handleClick,
    mouseover : handleMouseover,
    mouseout : handleMouseout
});
&lt;/pre&gt;&lt;/code&gt;


NOTE: When using the &quot;on&quot; method, the functions corresponding to the event names can be either anonymous functions or references to named functions. In the case of the &quot;un&quot; method, however, the functions MUST be named function references.

The full plugin:
&lt;code&gt;&lt;pre&gt;
(function($) {
   
    $.fn.on = function(eventMap /* Object */) {
        return this.each(function() {
            var elem = $(this);
            // Iterate through the map, binding each event to each handler
            $.each(eventMap, function(type, handler) {
                elem.bind(type, handler);
            });
        });
    };
   
    $.fn.un = function(eventMap /* Object */) {
        return this.each(function() {
            var elem = $(this);
            // Iterate through the map, unbinding each handler
            $.each(eventMap, function(type, handler) {
                elem.unbind(type, handler);
            });
        });
    };
   
})(jQuery)
&lt;/pre&gt;&lt;/code&gt;

Keep up the good work!</description>
		<content:encoded><![CDATA[<p>Nice work, Karl! I tend to agree that this sort of functionality shouldn&#8217;t be in the core, especially considering how easy it is to implement on one&#8217;s own. I recently developed a plugin for binding multiple events (both native and custom) in a single call. What I liked about this approach is that a.) only one short-hand method is needed, and b.) the plugin is not coupled to event names at all, allowing future support for new/custom events.</p>
<p><code>
<pre>
$.fn.on = function(eventMap /* Object */) {
    return this.each(function() {
        var elem = $(this);
        // Iterate through the map, binding each event to each handler
        $.each(eventMap, function(type, handler) {
            elem.bind(type, handler);
        });
    });
};
</pre>
<p></code></p>
<p>Usage is as follows:<br />
<code>
<pre>
$("#someID").on({
    click : function(e) {
        // Handle click
    },
    mouseover : function(e) {
        // Handle mouseover
    },
    mouseout : function(e) {
        // Handle mouseout
    }
});
</pre>
<p></code></p>
<p>Or, with named function references:<br />
<code>
<pre>
$("#someID").on({
    click : handleClick,
    mouseover : handleMouseover,
    mouseout : handleMouseout
});

function handleClick(e) {
	// Handle Click
}

function handleMouseover(e) {
	// Handle Mouseover
}

function handleMouseout(e) {
	// Handle Mouseout
}
</pre>
<p></code><br />
Implementing an unbinding method is equally trivial:<br />
<code>
<pre>
$.fn.un = function(eventMap /* Object */) {
    return this.each(function() {
        var elem = $(this);
        // Iterate through the map, unbinding each handler
        $.each(eventMap, function(type, handler) {
            elem.unbind(type, handler);
        });
    });
};
</pre>
<p></code></p>
<p>Which enables unbinding in the same manner:<br />
<code>
<pre>
$("#someID").un({
    click : handleClick,
    mouseover : handleMouseover,
    mouseout : handleMouseout
});
</pre>
<p></code></p>
<p>NOTE: When using the &#8220;on&#8221; method, the functions corresponding to the event names can be either anonymous functions or references to named functions. In the case of the &#8220;un&#8221; method, however, the functions MUST be named function references.</p>
<p>The full plugin:<br />
<code>
<pre>
(function($) {

    $.fn.on = function(eventMap /* Object */) {
        return this.each(function() {
            var elem = $(this);
            // Iterate through the map, binding each event to each handler
            $.each(eventMap, function(type, handler) {
                elem.bind(type, handler);
            });
        });
    };

    $.fn.un = function(eventMap /* Object */) {
        return this.each(function() {
            var elem = $(this);
            // Iterate through the map, unbinding each handler
            $.each(eventMap, function(type, handler) {
                elem.unbind(type, handler);
            });
        });
    };

})(jQuery)
</pre>
<p></code></p>
<p>Keep up the good work!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JavaScript links (week of 2009-07-25) at Scrufus</title>
		<link>http://www.learningjquery.com/2009/06/shorthand-methods-for-unbind/comment-page-1#comment-77912</link>
		<dc:creator>JavaScript links (week of 2009-07-25) at Scrufus</dc:creator>
		<pubDate>Sun, 26 Jul 2009 03:00:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.learningjquery.com/?p=798#comment-77912</guid>
		<description>[...] Shorthand methods for unbind [...]</description>
		<content:encoded><![CDATA[<p>[...] Shorthand methods for unbind [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rubens Mariuzzo</title>
		<link>http://www.learningjquery.com/2009/06/shorthand-methods-for-unbind/comment-page-1#comment-77823</link>
		<dc:creator>Rubens Mariuzzo</dc:creator>
		<pubDate>Wed, 08 Jul 2009 18:53:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.learningjquery.com/?p=798#comment-77823</guid>
		<description>I like the idea of keeping the jQuery API clean, and provides these functionalities in a plugin as you do. I&#039;m considering using it. Thanks.</description>
		<content:encoded><![CDATA[<p>I like the idea of keeping the jQuery API clean, and provides these functionalities in a plugin as you do. I&#8217;m considering using it. Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ariel Flesler</title>
		<link>http://www.learningjquery.com/2009/06/shorthand-methods-for-unbind/comment-page-1#comment-77816</link>
		<dc:creator>Ariel Flesler</dc:creator>
		<pubDate>Wed, 08 Jul 2009 04:10:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.learningjquery.com/?p=798#comment-77816</guid>
		<description>Really... I think we should expose that array of events in the core. I&#039;ve used it in plugins like 2 or 3 times at least.
Great post.</description>
		<content:encoded><![CDATA[<p>Really&#8230; I think we should expose that array of events in the core. I&#8217;ve used it in plugins like 2 or 3 times at least.<br />
Great post.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Karl Swedberg</title>
		<link>http://www.learningjquery.com/2009/06/shorthand-methods-for-unbind/comment-page-1#comment-77814</link>
		<dc:creator>Karl Swedberg</dc:creator>
		<pubDate>Tue, 07 Jul 2009 22:29:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.learningjquery.com/?p=798#comment-77814</guid>
		<description>Ugh. What a mess. Thanks, Matthew, for pointing this out. I shouldn&#039;t have rewritten the script without testing it right before I published the entry.</description>
		<content:encoded><![CDATA[<p>Ugh. What a mess. Thanks, Matthew, for pointing this out. I shouldn&#8217;t have rewritten the script without testing it right before I published the entry.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matthew Crumley</title>
		<link>http://www.learningjquery.com/2009/06/shorthand-methods-for-unbind/comment-page-1#comment-77813</link>
		<dc:creator>Matthew Crumley</dc:creator>
		<pubDate>Tue, 07 Jul 2009 21:25:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.learningjquery.com/?p=798#comment-77813</guid>
		<description>You actually need capture the current value of &lt;code&gt;i&lt;/code&gt; or the corresponding &lt;code&gt;eventType&lt;/code&gt; in each iteration of the loop. The way it is now, all the un&lt;em&gt;whatever&lt;/em&gt; functions would call &lt;code&gt;this.unbind(undefined, fn)&lt;/code&gt; because each function keeps a reference to the single &lt;code&gt;i&lt;/code&gt; variable, which is equal to &lt;code&gt;etlength&lt;/code&gt; at the end of the loop. The loop should look like this:
&lt;pre&gt;&lt;code&gt;for (var i=0; i &lt; etLength ; i++) {
    $.fn[ &#039;un&#039; + eventTypes[i] ] = (function(type) {
        return function(fn) {
            return this.unbind( type, fn );
        };
    })(eventTypes[i]);
}&lt;/code&gt;&lt;/pre&gt;
Of course, you could also factor the outer anonymous function into a &lt;code&gt;makeUnbind&lt;/code&gt; function or something like that since it doesn&#039;t need to close over any local variables.</description>
		<content:encoded><![CDATA[<p>You actually need capture the current value of <code>i</code> or the corresponding <code>eventType</code> in each iteration of the loop. The way it is now, all the un<em>whatever</em> functions would call <code>this.unbind(undefined, fn)</code> because each function keeps a reference to the single <code>i</code> variable, which is equal to <code>etlength</code> at the end of the loop. The loop should look like this:</p>
<pre><code>for (var i=0; i &lt; etLength ; i++) {
    $.fn[ 'un' + eventTypes[i] ] = (function(type) {
        return function(fn) {
            return this.unbind( type, fn );
        };
    })(eventTypes[i]);
}</code></pre>
<p>Of course, you could also factor the outer anonymous function into a <code>makeUnbind</code> function or something like that since it doesn&#8217;t need to close over any local variables.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced (User agent is rejected)
Database Caching 9/23 queries in 0.008 seconds using disk: basic
Object Caching 373/384 objects using disk: basic
Content Delivery Network via learningjquery.kswedberg.netdna-cdn.com

Served from: www.learningjquery.com @ 2012-02-08 14:32:49 -->
