Hello I am learning the source code of react Scheduler part.
I understand the concept part to some extent, but I don’t understand the code below.
// Capture local references to native APIs, in case a polyfill overrides them.
const localSetTimeout = typeof setTimeout === 'function' ? setTimeout : null;
const localClearTimeout =
typeof clearTimeout === 'function' ? clearTimeout : null;
const localSetImmediate =
typeof setImmediate !== 'undefined' ? setImmediate : null; // IE and Node.js + jsdom
When I thought about it, I think I could use it after inspecting setTimeout, clearTimeout, and setImmediate itself, but I wonder why there is a code like this.
Of course, the comments say to prepare for overriding of polyfills, but I don’t understand exactly what situation you’re talking about.
Any help understanding the code would be appreciated.
>Solution :
Well let me explain to you as simply as I can. The source code we have here captures references to native APIS such as (setTimeout, clearTimeout etc..) and stores them in local variables.
This procedure is done when a polyfill library overrides these APIs but with its own implementations.
Now what is a polyfill
Its a piece of code that provides a feature that is not supported in a native way although it is required by the code. It provides some kind of compatibility for older browsers.
You could say that this code is a defensive programming technique that ensures that the code works correctly in all environments, even if a polyfill overrides native APIs.
I hope it helps