DEV Community

Discussion on: Mutates or not? We need both versions but there is a problem.

lukeshiru profile image

I have a "strong opinion" against mutation, but still, this comment is not based on that:

Your idea is not possible because, for example, Array.prototype.flat returns a flatten copy, and will always return that. One of the rules of JS is "don't break the web", so methods that have a behavior, will keep having that behavior, even if it doesn't make much sense. Take null as an example: We all know by now that in JS typeof null returns "object", but the fact is that it should’ve returned "null" but the first implementation of JS had a bug on a switch case that they use for the typeof function, and they messed up returning "object", and now it can't be changed without "breaking the web". Same applies to everything, so the only option is to add new stuff, not to change the stuff that's already there like Array.prototype.flat or Array.prototype.filter.

I would love the exact oposite to happen, and for example get Array.prototype.reverse to be changed so it doesn't mutate, but sadly we can't, so that's why we are getting Array.prototype.withReversed instead.


michalkuncio profile image
Michał Kuncio Author

Hi! Yes, that's why I mentioned that the best idea for me wouldn't be really possible because of backwards compatibility. I guess for now the solution from TC39 proposal is the most realistic option we have. Cheers!