15 ms·
Neat! Extension methods and getting rid of "var that = this;" in one! It seems that it also extends to full expressions on the right-hand side of ::, so you ca
by rfw 10y ago
Neat! Extension methods and getting rid of "var that = this;" in one!
It seems that it also extends to full expressions on the right-hand side of ::, so you can even do, for using Array operations over things that look like arrays but not quite:
document.querySelectorAll('p')::([].map)(f)
instead of:
[].map.call(document.querySelectorAll('p'), f)
(replace [].map with Array.prototype.map if it feels less gross)
- deleted 10y ago[deleted]
- mschuetz 10y agoI find ".bind(this)" to be preferable over "var that = this" anyway.
- tomatsu 10y agoThis also works, by the way: [...document.querySelectorAll('p')].map(f) In the future, NodeList could extend Array which would make these workarounds redundant. Extending Array was made possible by ES6. Anyhow, I think having some kind of extension methods would be great.
- gsnedders 10y ago> In the future, NodeList could extend Array which would make these workarounds redundant. It would break the web, unfortunately. (We've experimented with NodeList.prototype.__proto__ being Array.prototype instead of Object.prototype and it breaks sites, and ES6's extends notion implies that.)
- cjdkdnxnsnd 10y agoWow, what an improvement! In fact, why don't we just drop keywords altogether and adopt APL-like syntax for better code concision? Here, let me offer an example of how superior APL is: (~R∊R∘.×R)/R←1↓ιR
- Achshar 10y agoWhy do that in the callback way at all? Why not simple for(const el of document.querySelectorAll('p')) {} Why have the overhead of a callback to begin with. It reads SO much better as well.
- gliese1337 10y agoIt has different semantics. Your code is equivalent to a `forEach` call, not `map`. Calling `map` returns a new array containing the results of the callback, while a for loop or a `forEach` don't.