views:

532

answers:

8

Pouring over the release notes regarding jQuery 1.4, I came acrosss $.noop() which is:

Description: An empty function. (added in 1.4)

You can use this empty function when you wish to pass around a function that will do nothing.

Perhaps I'm missing something profound here, but what exactly is a practical use of passing around an empty function?

Code examples appreciated.

A: 

The only logical reason is if you're calling a function that does something AND calls another function, and you want the higher-level function to do its thing without calling a parameter function.

Most of the jQuery functions optionally take a parameter function, so you don't have to pass one in. Maybe there's one or two where that's not the case -- or maybe it's to assist developers with their custom code that behaves like this.

Kaleb Brasee
A: 

If a function requires you pass a function as an argument maybe? It's shorter to say do_something($.noop) than do_something(function(){}).

Although not by much...

...6 characters...

...yeah, that feature looks quite useless actually.

Nicolás
It avoids the overhead of a new function.
SLaks
I wouldn't count on that, SLaks - check out the code for noop in the source:noop: function() {},
Alex JL
@Salsa: but it's always the same function. Everywhere $.noop() is used, it's the same, a single instance of a single do-nothing function. I doubt there's enough overhead in most JS VMs for this to make any noticeable difference, but in theory...
Shog9
Actually SLaks is right. The function assigned to `noop` is created once when the jquery library is loaded. foo(function(){}) creates a new function object each time that line is executed.However, *nobody cares*, since the rest of your JS code would take way more time in comparison.
Nicolás
Okay, that's a good point. If you want to use an empty function, no need to create a new one each time since one already exists. It won't affect whatever the overhead of calling it is though, I presume.
Alex JL
See the links in CMS's answer. Turns out, this *can* make a difference...
Shog9
A: 

Probably if some bad API requires a function as a parameter, and you don't want to do anything in it, this would be a framework-supported way of making that obvious.

Noon Silk
Indeed, this is how it appears to be used in jQuery itself - as an obvious placeholder when no real callback is available.
Shog9
+6  A: 

If you have a function that accepts a function as a parameter, and you don't have any code to give it, you can pass $.noop.

I can't think of any such cases in jQuery where the parameter isn't optional in the first place, though.

Unlike writing function(){}, passing $.noop will not create a new function instance, saving a bit of memory. However, if whatever you're passing it to modifies the function object (eg, funcParam.id = 2), passing $.noop will mess things up.

SLaks
It's nice not to have to check every passed function parameter for undefined before executing, or when calling the function, to have to provide function(){} every time.
Tony k
+1  A: 

It's purely a convenience/replacement for function(){} in the context of where callbacks are required - I don't think I'll be using it anytime soon.

I bet the jQuery team had quite a laugh when they dropped it in though, also serves a comedic purpose.

meder
A: 

It can be useful if you have a function that supplies functions to other functions.

Example: You have a List of data. Each item has a Button that does something. The "something" can be different for every item. You have a "FunctionFactory" that takes in the item and returns a function. If you don't want the button to do something for whatever reason, then the cleanest way could be to return an empty function, as that way you know that your Factory ALWAYS returns a function.

I don't have a concrete example for jQuery, but I guess this could come in handy when used in an .each or .map block.

Michael Stum
+1  A: 

I use a couple of plugins which require callbacks, but for some parts I don't actually want to use a certain callback. So, I put in function() {}.

noop is defined in the jQuery source as

noop: function() {}

so it will fit anywhere you'd use a blank function, such as the above example.

Alex JL
+14  A: 

This function was proposed due performance issues on embedded systems when using $.ajax, reported on the jQuery-Dev Mailing list, you can see the thread.

Basically they preferred to introduce and use a this single empty function, rather than declaring empty anonymous functions all around.

Now this function is internally use in the ajax, event and offset modules.

You can give a look to the commit when it was introduced also.

CMS
Embedded systems have no business running Javascript anyway.
Nicolás