"Implement your own" is a series of JavaScript exercises where I try to rewrite existing JavaScript features and classes in order to understand them better. The original idea comes from a YouTube creator called Web Dev Simplified. Kyle from WDS has amazing educational content revolving mainly around JavaScript and web development.
In one of his videos, he implemented some of the standard array functions that are built in the JavaScript language, such as:
and so on. I thought this was a great exercise and wanted to try it for myself before continuing to watch the video. I also think this is a good way of showing how deep is your understanding of the language. It's easy to read the manual and use something, but can you build it from scratch?
Another WDS video was about rewriting the Promise class in JavaScript. I'm talking about methods like:
and even static methods:
The thing about rewriting these methods and classes is that it's fairly easy to test them. To see if your method works correctly, you compare your results with the results you got from the actual method you're rewriting. In the case of Promises, you can replace the Promise class with your implementation called MyPromise and see if you get the same outcome.
Speaking of testing, something really convenient about Kyle's videos of this sort is that he provides a test file for each of these implementations. All you have to do is write the code and run the tests to see if you're correct. Very clean and convenient — and if you're interested to try this out for yourself, you can find the test file for the Array and Promise implementations here and here, respectfully.
He's using the standard Jest
testing library and even though the tests don't cover 100% of the actual codebase, it's more than enough for the scope of this exercise. It's also very satisfying to see all the green colors after you run the tests!