I can't help but notice that it's impossible to do something like
["cat", "dog"].map(String.prototype.toUpperCase);
which throws
Uncaught TypeError: String.prototype.toUpperCase called on null or undefined
The following code works (in es6 arrow notation for my typing convenience), but seems weirdly indirect:
["cat", "dog"].map(s=>s.toUpperCase())
Weirdly indirect because it creates an extra anonymous function just to wrap a perfectly good function that already exists. And maybe this is something that one must live with, but it doesn't taste right.
So I have two questions:
Is there a direct way to map a string method over an array of strings without wrapping it in an anonymous function?
What's the technical explanation for why the code I tried first doesn't work? I have two guesses, but don't know which is right.
Guess (a): this is because of boxing, i.e., string literals aren't the same as string objects, and for some reason mapping the method over them doesn't do the same kind of quiet coercion that just calling the method on the string does.
Guess (b): this is because while functions are first class objects, methods aren't in the same way, and can't ever be mapped?
Guess (c): there's some other syntax I should be using?! ("".prototype.toUpperCase
??) Because of JavaScript gremlins? Because null both is and is not an object? Fixed in ES2025? Just use lodash and all will be cured?
Thanks!