6

I was going through one Backbone.js plugin in which I found the below piece of code.

callbacks : {
    search : $.noop,
    valueMatches : $.noop
}

What is the $.noop() function doing here?

Vivek Jain
  • 3,811
  • 6
  • 30
  • 47
RRR
  • 3,509
  • 4
  • 29
  • 38
  • 1
    it is an [empty function](http://api.jquery.com/jQuery.noop/), which doesn't do anything, this is used so that you don't have to check whether the option is undefined before calling it – Arun P Johny Feb 05 '14 at 09:06
  • 2
    Why not using `function() {}` directly? Because there is already a cached one used by jQuery already. :D – stevemao Jun 05 '15 at 05:30
  • 1
    Possible duplicate of [What real purpose does $.noop() serve in jQuery 1.4?](http://stackoverflow.com/questions/2069345/what-real-purpose-does-noop-serve-in-jquery-1-4) – Pang Sep 19 '16 at 07:57

1 Answers1

9

$.noop is an empty function so in your case it's returning an empty function

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

This is useful for plugin authors who offer optional callbacks; in the case that no callback is given, something like jQuery.noop could execute.

Documentation found here : http://api.jquery.com/jquery.noop/

Anton
  • 32,245
  • 5
  • 44
  • 54