When programming in JavaScript, we can use the function as a parameter for a specific operation. For example, a function may have a callback that is called after an event.
function doIt(callback) {
Now, if this callback is an optional parameter and it is not specified, we will get the error Uncaught TypeError: callback is not a function , because the Uncaught TypeError: callback is not a function undefined. There are two solutions to this problem. The obvious is to check the callback using the if . Another option is to set the empty function as the default callback if it is not assigned. This approach is very useful and shines if we have several places that call the callback function. Therefore, we do not need to check it for undefined every time before calling it.
function doIt(callback) { callback = callback || function(){};
Similarly, there are many use cases where we can have variables of redefined functions. This is a common template for setting these types of variables as default values so that we can call them up without worrying about whether they are assigned / canceled or not.
In addition, in some special cases, it is useful to have functions that do nothing but return a specific value. makeEmptyFunction used to create such functions. Basically, it returns a function that does nothing, but returns what the parameter has ever passed.
emptyFunction.thatReturnsFalse = makeEmptyFunction(false);
As you can see in the file, the above code generates an empty function that returns false .
"null call idiom" is what we find in Objective-C / Cocoa programming. This basically allows you to call the uninitialized object method (null pointer) without any errors, as in most other languages. I think the author tried to explain in a comment.
Since JavaScript does not have such a language function, we explicitly achieve it with empty functions. Some people call it no-op or noop, and you can find similar helpers in other popular JavaScript libraries such as JQuery and AngularJS .