Function constants for Identity and No-op

Tab Atkins Jr. jackalmage at gmail.com
Wed Aug 10 17:32:53 UTC 2016


On Wed, Aug 10, 2016 at 7:25 AM, Eli Perelman <eli at eliperelman.com> wrote:
> Now obviously it would be trivial for me to declare these constants in
> userland code like I already do, e.g. `const NOOP = () => {}`, but in
> projects where it's needed in several files, I'll have to put that in a
> module or re-declare everywhere. This is not a huge inconvenience but
> something that could easily allocated for in the language.

Mark's argument (which I agree with) is that `x=>x` and `()=>{}` are
good *spellings* of "identity function" and "noop function".  It's
immediately obvious what they do; `obj.doCallback(()=>{})` is about as
easy to understand as `obj.doCallback(Function.noop)`.  The ID
function is even simpler - `obj.map(x=>x)` reads *extremely* well to
me, equal or better than `obj.map(Function.id)`. If we were to reserve
"id" and "noop" as bare global variables, I might agree that those
were even better, but that's clearly out of the question.

~TJ


More information about the es-discuss mailing list