What is the purpose of makeEmptyFunction in fbjs?

I looked at the source of React and came across a requirement with var emptyFunction = require('fbjs/lib/emptyFunction'); .

I looked at this feature and was confused by what it does.

Here is the function

 function makeEmptyFunction<T>(arg: T): (...args: Array<any>) => T { return function() { return arg; }; } const emptyFunction: (...args: Array<any>) => void = function() {}; 

In the comments, they provide the following explanation, which confused me:

This function accepts and discards input; It has no side effects. This is primarily useful idiomatically for a redefined function endpoints that should always be callable since JS does not have a null call. Idiom ala Cocoa

I never came across a zero idiom and hoped that someone could clarify what that means and explain the purpose of this function in a less technical language.

Hopefully this issue will not be addressed because it is not entirely related to the code. Maybe it belongs somewhere else if I'm so sorry.

+5
source share
1 answer

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) { // some work callback(); // more work } 

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(){}; // some work callback(); // more work } 

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 .

+4
source

Source: https://habr.com/ru/post/1270819/


All Articles