1

If I want to apply a function to a single object, I can do it like this:

> 'mystr'.toUpperCase()
'MYSTR'

But if I want to do the same for an array, using map (for example), I must do it like this:

> ['one','two','three'].map((x)=>x.toUpperCase(x))
[ 'ONE', 'TWO', 'THREE' ]

Is there a way to use this functions without the dot notation? For example

// THIS DOESN'T WORK, BUT I WANT TO ACHIEVE SOMETHING LIKE THIS
> String.toUpperCase('mystr')
'MYSTR'
> ['one','two','three'].map(String.toUpperCase)
[ 'ONE', 'TWO', 'THREE' ]

These are just examples, I don't give me specific solutions for using map or toUpperCase, please. I just want to know if member functions can be used as callback functions without an anonymous extra function

Miguel
  • 2,130
  • 1
  • 11
  • 26
  • 2
    `String.toUpperCase` still uses dot notation... – jonrsharpe Jun 17 '21 at 12:42
  • Do you mean `String.prototype.toUpperCase.call("mystr")`? or perhaps something like `function toUpper(text) { return typeof text === "string" ? text.toUpperCase() : ""; }` and `toUpper("mystr")` – nick zoum Jun 17 '21 at 12:43
  • Yes, sorry. I mean that you don't need an existing object to put the dot and then apply it. I'll try to rephrase that. – Miguel Jun 17 '21 at 12:44

2 Answers2

1

Not without creating another function, no. You'll end up creating a function either way.

But it can be a reusable one:

// Reusable function
const makeUpper = str => str.toUpperCase();

// Using it
console.log(["one","two","three"].map(makeUpper));

You can also create the function with bind, but it's pretty convoluted to read:

// Reusable function
const makeUpper = Function.prototype.call.bind(String.prototype.toUpperCase);

// Using it
console.log(["one","two","three"].map(makeUpper));
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Wouldn't it more proper (and less confusing) to do `Function.prototype.call.bind(String.prototype.toUpperCase)`? Or `$.call.bind("".toUpperCase)` for short. – nick zoum Jun 17 '21 at 13:01
  • @nickzoum - I wouldn't call it more or less proper. I don't know whether that would confuse people more or less. (But certainly not the `$` version, there's no jQuery here. :) ) I do kind of like `Function.prototype.call` better, yeah. – T.J. Crowder Jun 17 '21 at 15:20
  • I meant the `Function.prototype.call` was more appropriate, the `$` part was just a short option. Pretty sure all the browsers have a short `$` implementation, which only returns the first element found, instead of the full list, can't find any documentation on it though. – nick zoum Jun 17 '21 at 16:09
  • 1
    @nickzoum - Only in the console, not in actual code. Try this in various browsers: https://jsfiddle.net/tjcrowder/ytmdupLn/ – T.J. Crowder Jun 17 '21 at 16:10
0

map calls the function that you pass it for every item in the array passing the current item as the first argument.

Functions like toUpperCase don't take any arguments (in your example you pass x but it is ignored). They expect the value they operate on to be this.

So, no. You can't just pass a function which expects this to be significant to map.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335