What is the real purpose of the $ .noop () function in jQuery 1.4?

When issuing jQuery 1.4 release notes, I met $.noop() , which:

Description : empty function. (added in 1.4)

You can use this empty function if you want to pass a function that does nothing.

Perhaps I am missing something deep here, but what exactly is the practical use of bypassing an empty function?

Code examples are indicated.

+43
javascript jquery noop
Jan 15 '10 at 3:40
source share
9 answers

This feature was proposed due to embedded system performance issues when using the $.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.

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

You can take a look at commit when it was introduced as well.

+47
Jan 15 '10 at 4:10
source share

If you have a function that takes a function as a parameter, and you don’t have code to pass it, you can pass $.noop .

I cannot think of such cases in jQuery where the parameter is not optional, however.

Unlike the function(){} entry, passing $.noop will not create a new instance of the function, saving a bit of memory. However, if you pass it to modify the object of the function (for example, funcParam.id = 2 ), passing $.noop will be ruined.

+18
Jan 15
source share

An example of the real world (well, almost):

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

Use it instead of function (){}

+8
Jun 28 2018-11-11T00:
source share

Perhaps if some bad API requires a function as a parameter, and you don't want to do anything about it, this will be a framework-supported way to make this obvious.

+3
Jan 15
source share

I use a couple of plugins that require callbacks, but for some parts I actually don't want to use a specific callback. So, I introduced the function () {}.

noop is defined in jQuery source as

 noop: function() {} 

therefore, it will be located anywhere you would use an empty function, for example, in the example above.

+2
Jan 15
source share

The only logical reason is that you call a function that does something and calls another function, and you want the higher-level function to do its job without calling the parameter function.

Most jQuery functions do not necessarily accept a parameter function, so you do not need to pass it. Maybe there’s one or two where it’s not, or maybe it will help developers with their own code that behaves like that.

0
Jan 15
source share

Should a function require you to pass a function as an argument? In short, do_something($.noop) than do_something(function(){}) .

Although not much ...

... 6 characters ...

... yes, this feature seems completely useless in reality.

0
Jan 15
source share

This is just a convenience / replacement for function(){} in the context of where callbacks are required - I don't think I will use it any time soon.

I bet the jQuery team laughed pretty when they dropped it, although it fulfills a comedic goal.

0
Jan 15 '10 at 3:45
source share

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

Example: you have a list of data. Each element has a button that does something. Something may be different for each item. You have a "FunctionFactory" that takes an element and returns a function. If you do not want the button to do something for any reason, the cleanest way would be to return an empty function, since you know that your Factory ALWAYS returns the function.

I don't have a specific example for jQuery, but I think it might come in handy when used in a .each or .map block.

0
Jan 15
source share



All Articles