I was no here when that proposal was wrote. But those methods are new and we still have time to fix this.
The change array by copy
proposal is harmful for JS/TS.
Since its release, the learning curve for splice
, sort
and reverse
got duplicated by their unmutable contraparts.
What will happen if we want to add a new "self-mutable" method to arrays in the future? Will we need to create an unmutable method too? How much methods we really want within our arrays?
Also, those methods are no self-descriptive at all, at first sight you won't know that are copying their objects.
I'd like to replace those redundant methods with a single one called .clone
.
This method will (of course) clone the array (or tuple) to avoid modify the original one.
[1, 2, 3, 4].clone().reverse() // [4, 3, 2, 1]
If we want to clone AND MODIFY in a single step (avoid clone and then modify), has the involved proposal does, then we could add an argument that will accept the action to perform:
.clone(Array.prototype.reverse)
or .clone("reverse")
to create a new array with their items reversed.
["Hi", "how", "are", "you", "?"].clone("reverse") // ["?", "you", "are", "how", "Hi"]