Function constants for Identity and No-op

Michał Wadas michalwadas at gmail.com
Wed Aug 10 11:42:00 UTC 2016


Function.prototype is no-op (run Function.prototype.toString() for more
information)

On Wed, Aug 10, 2016 at 12:03 PM, Tiddo Langerak <tiddolangerak at gmail.com>
wrote:

> I think this is more about improving readability than saving a few
> characters.
>
>
> On 08/10/2016 11:10 AM, Isiah Meadows wrote:
>
> I'll note that it's longer than just typing them out manually (and close
> if they're aliased):
>
> ```js
> Function.IDENTITY
> IDENTITY
> x => x
>
> Function.NOOP
> NOOP
> () => {}
> ```
>
> Not sure if it adds anything.
>
> On Tue, Aug 9, 2016, 14:44 Eli Perelman < <eli at eliperelman.com>
> eli at eliperelman.com> wrote:
>
>> I'm not sure if something like this has been proposed before, but I often
>> find myself declaring constants for the identity function and the no-op
>> function. I think it would be useful to have constants available on
>> Function containing these values. For example `Function.IDENTITY` and
>> `Function.NOOP`. Essentially these constants would map to:
>>
>> ```js
>> Function.IDENTITY = (a) => a;
>> Function.NOOP = () => null; // or:
>> Function.NOOP = () => {};
>> ```
>>
>> These could then be used in places where non-user-controlled APIs need
>> default functions need to be executed, or as placeholders for default
>> values that may be executed before a function has been supplied. For
>> example:
>>
>> ```js
>> // third-party API requires a callback or throws, but no functionality
>> really needs to done:
>> thirdParty.action(Function.NOOP);
>>
>> // Function needs a default which *may* execute prior to having a
>> different one specified (contrived):
>> const action = (handler = Function.IDENTITY, value = 10) =>
>> handler(value);
>> ```
>>
>> Thoughts? Seems like something simple with positive value. Thanks!
>>
>> Eli Perelman
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>
>
> _______________________________________________
> es-discuss mailing listes-discuss at mozilla.orghttps://mail.mozilla.org/listinfo/es-discuss
>
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160810/36b42a8d/attachment-0001.html>


More information about the es-discuss mailing list