49

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.

0

9 Answers 9

52

This function was proposed due to 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 this single empty function, rather than declaring empty anonymous functions all around.

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

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

0
20

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.

1
  • 1
    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.
    – Anthony
    Jan 15, 2010 at 3:58
10

Real World Example (well almost):

jQuery.fn.myAwesomeAjax = function(url, complete) {
  return jQuery.ajax(url || this.url)
    .complete(complete || jQuery.noop);
};

Use it instead of function (){}

4

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.

1
  • 1
    Indeed, this is how it appears to be used in jQuery itself - as an obvious placeholder when no real callback is available.
    – Shog9
    Jan 15, 2010 at 4:01
3

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.

0

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.

0

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.

5
  • 6
    It avoids the overhead of a new function.
    – SLaks
    Jan 15, 2010 at 3:48
  • 1
    I wouldn't count on that, SLaks - check out the code for noop in the source: noop: function() {},
    – JAL
    Jan 15, 2010 at 3:51
  • 3
    @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
    Jan 15, 2010 at 3:56
  • 2
    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
    Jan 15, 2010 at 3:56
  • 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.
    – JAL
    Jan 15, 2010 at 3:59
0

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.