In Python, you can something like this:
>>> list(map(str.upper, ['foo','bar']))
['FOO', 'BAR']
I would like to be able to do something similar in javascript:
I've tried the following in Chrome using the native map implementation ( https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map ):
['foo','bar'].map(String.prototype.toUpperCase)
['foo','bar'].map(String.prototype.toUpperCase.call)
Why doesn't call work? Is there any elegant way to do this or must I wrap toUpperCase in a callback function? Thx
toUpperCase.calldoesn't work becausecallis resolved againstFunction.prototypeand so is not related totoUpperCase. It looks atthisto figure out which method is being called, and the waymapinvokes the function,thisisundefined.