"Method Group" - a view of syntax in Javascript?

C # has an amazing syntax function, where for a function that expects a delegate type, you can pass a "group of methods", for example:

"string".Count (Char.IsWhiteSpace);

and not (relatively speaking) noise:

"string".Count (c => Char.IsWhiteSpace (c));

(Edit: best example).

I would like to do something similar in Javascript, which has a much more noisy syntax for anonymous functions:

var name = "foobar".replace (/^\w/, function (c) { return c.toUpperCase (); });

I played with various attempts and permutations of arguments in functional form String.prototype.replaceusing calland apply, but the string argument passed in ( cin the above example) is apparently not thisin the area inside toUpperCase(at best, I get less useful DOMWINDOWoobarand TypeErrorat worst).

, , , , - , ?

+3
1

, String

"foobar".replace (/^\w/, String.toUpperCase );

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/toUpperCase

: FF

0

Source: https://habr.com/ru/post/1792094/


All Articles