One interesting syntactical feature of C# is the ability to pass in a "method group" to a function expecting a delegate type, like this:
"string".Count (Char.IsWhiteSpace);
This is much cleaner compared to using an anonymous function:
"string".Count (c => Char.IsWhiteSpace (c));
In Javascript, the syntax for anonymous functions can be quite noisy. I would like to achieve a similar simplicity for anonymous functions in Javascript:
var name = "foobar".replace (/^\w/, function (c) { return c.toUpperCase (); });
I have experimented with different approaches using call
and apply
on the functional form of String.prototype.replace
, but it seems that the string argument passed in (c
in the example) is not correctly used as this
within toUpperCase
.
Although I can wrap the function as I did before, I am curious if there is a way to achieve method group application or properly invoke the function on matched characters without encountering errors.