Array.prototype.toggle

Claude Pache claude.pache at gmail.com
Fri Feb 7 13:58:09 UTC 2020



> Le 7 févr. 2020 à 14:52, Claude Pache <claude.pache at gmail.com> a écrit :
> 
> 
> 
>> Le 7 févr. 2020 à 13:23, manuelbarzi <manuelbarzi at gmail.com <mailto:manuelbarzi at gmail.com>> a écrit :
>> 
>> calling it again would just apply the same mechanism => "do this element already exist? "remove it" : "add it". in the example, it would just add 1 at the end of array (i.e. just a push), calling it the second time.
> 
> Probably `Array.prototype.remove` would be a more appropriate name for what you suggest.

Sorry, I somewhat misread your message. So:

The DOMTokenList array-like interface has a `toggle` method, and some others like `add`, `remove`, etc.

https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList <https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList>

Although, as I said, in such situations, I tend to use a Set instead of an Array.

Maybe it would make sense to add a `toggle` method to `Set.prototype`?

—Claude

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20200207/50cf7dd0/attachment-0001.html>


More information about the es-discuss mailing list