In the realm of JavaScript, arrays serve as the backbone of many operations. They offer a flexible structure to store and manipulate data efficient...
For further actions, you may consider blocking this person and/or reporting abuse
Thank you for the article! I didn't even know you could make custom array functions.
I get a little lost with the callback parameter in the customFilter. What does this line mean:
callback(this[i], i, this)
Thank you for your response!!
In the line callback(this[i], i, this), the code is invoking the callback function that is passed as an argument to the customFilter method.
So, callback(this[i], i, this) is passing three arguments to the callback function:
In the context of the example usage,
The callback function checks if a number is even by evaluating num % 2 === 0. It only returns true for even numbers, which means it filters out odd numbers when used with the customFilter method.
It's generally considered bad practice to do this, as there is no guarantee that the methods you create will not conflict with other libraries, or with future extensions to the JS language itself.
The only safe way that I know of to add methods to native prototypes is to name those methods with Symbols as the names of the methods - which guarantees that they will not conflict with anything. In fact, I built a whole library based around this idea:
Introducing Metho: Safely adding superpowers to JS
Jon Randy 🎖️ ・ Oct 12 '21
Another way to go would be to make your own array class that subclasses and extends the original array class.