What real purpose does $.noop() serve in jQuery 1.4?
Solution 1:
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.
Solution 2:
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.
Solution 3:
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 (){}