Because Javascript is a "prototype" oriented language (although, modern introduction of Class keyword, and the rise of TypeScript has obscured that somewhat) you can simply do this which was even considered idiomatic a decade ago:
Array.prototype.reverse = function() { let start = 0; let end = this.length - 1; while (start < end) { // Swap elements let temp = this[start]; this[start] = this[end]; this[end] = temp; // Move to the next pair start++; end--; } return this; }; // Example usage: const arr = [1, 2, 3, 4, 5]; arr.reverse(); // [5, 4, 3, 2, 1] Lee Hambley http://lee.hambley.name/ +49 (0) 170 298 5667 On Sun, 18 Aug 2024 at 15:46, DAZ <daz4...@gmail.com> wrote: > What are people's thoughts about monkey patching JavaScript to make it > more like Ruby? > > I find myself constantly missing being able to use some of Ruby's array > methods ... and the fact that you can't do "hello".reverse is just > ridiculous! > > It would be really nice to be able to do things like [1,2,3].any and > [1,2,3].empty and [1,2,3].sum instead of using a reduce! > > I know that monkey patching is more of a Ruby thing and frowned upon in > JavaScript. I've heard the arguments against it, but kind of think it's > worth it to have a nice experience coding in JS. > > There's a library called rearmed.js that does something like this, but I'd > like to have a go at writing my own. > > Any thoughts? > > -- > You received this message because you are subscribed to the Google Groups > "North West Ruby User Group (NWRUG)" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to nwrug-members+unsubscr...@googlegroups.com. > To view this discussion on the web, visit > https://groups.google.com/d/msgid/nwrug-members/a65512af-0b73-4cb0-8250-0b675bd07e1bn%40googlegroups.com > <https://groups.google.com/d/msgid/nwrug-members/a65512af-0b73-4cb0-8250-0b675bd07e1bn%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "North West Ruby User Group (NWRUG)" group. To unsubscribe from this group and stop receiving emails from it, send an email to nwrug-members+unsubscr...@googlegroups.com. To view this discussion on the web, visit https://groups.google.com/d/msgid/nwrug-members/CAN_%2BVLV1gZnJ1OjSRuHS9N%3DozshNr9PtAEqiRV_eQsdu_Sy%3DDw%40mail.gmail.com.