Should I be returning Listeners?

So, I was just playing around with seeing what a Uglifiers do to my code. And I noticed that it returns Listeners when I’ve really never done it that way. Should I be? What is the benefit or consequence from just using listeners inside of Objects?

This has always felt wrong, but never really saw any reason to change it since the event listeners are registered when the object is instantiated. (or am I wrong on this?)

var x = (function($) {
     var imTrue = true;

           alert('KEYS PRESSED');

     return {  data: 'some stuff' };


var y = (function($) {
     var imTrue = true;

     return $('.something').keyup(function(){
           alert('KEYS PRESSED');
     }), {
       data: 'some stuff'

If I am reading that correctly… I think you have a mistake in the second one and am assuming you are returning something like:

return $(".a").keyup(fn, {data: "some stuff"})

instead of:

return $(".a").keyup(fn), {data: "some stuff"};

if that is the case, those two examples are not equivalent. :confused: the second one will return the map {data: "some stuff"}, and I am fairly sure the first one will return an instance of jquery :confused: Which is not behaviour I would want my uglifier to do

2 Elements you missed this: }), { :smile: They work.

Sorry sorry sorry, I get whats going on.

An example:

They are using the comma as an infix operator. Think of it like an && operator but with a super low precedence and always returns the right hand argument. Its also the safest way to call eval if you have to as it will pull it out of its current scope and put it in the global scope. ie:


But! I dont know why they are doing it to your bindings. Are they only doing it to your binding functions or are they doing it to all the statements in inside of a block that returns something?

1 Like

I just did a perf test

, vs ;

Doesnt seem like its for a performance gain :confused:

though it was a very quickly done test :confused:

1 Like

didnt make any difference with the actual code you provided either.

1 Like

Just the event bindings. Everything else that was public was being returned anyway and that wasn’t really effected.

I mean it makes sense, because those are technically public elements and not private. So it makes me wonder if there is some other thing that I might be missing by not returning them from the object or it just does that for other reasons that have to do with minification.

Well it doesnt actually return them, it executes them and then returns the value on the right of the , operator.

AH! Right. That makes sense. Thanks.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.