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

两盒软妹~` 提交于 2019-12-03 05:13:55
CMS

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.

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.

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

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.

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.

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.

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!