What real purpose does $.noop() serve in jQuery 1.4?

前端 未结 9 635
故里飘歌
故里飘歌 2020-12-30 19:01

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

Description: An empty function. (added in 1.4)

相关标签:
9条回答
  • 2020-12-30 19:18

    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.

    0 讨论(0)
  • 2020-12-30 19:20

    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 (){}

    0 讨论(0)
  • 2020-12-30 19:26

    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 讨论(0)
提交回复
热议问题