3 ms·
OP's example in coffeescript: Array::compact = -> x for x in @ when x? In es6: Array.prototype.compact = function() { return this.filter(x => x !== n
by nwienert 12y ago
OP's example in coffeescript:
Array::compact = -> x for x in @ when x?
In es6:
Array.prototype.compact = function() {
return this.filter(x => x !== null);
}
I find it easier to read than OP's example, but I would like to see non-lexically bound this function shorthand in es6/7. I see a mention of it at the bottom of this spec[1], but not sure what they mean by "Initializer opt". Maybe like this:
Array.prototype.compact = this => this.filter(x => x !== null)
[1] http://tc39wiki.calculist.org/es6/arrow-functions/ http://tc39wiki.calculist.org/es6/arrow-functions/
- gumballhead 12y agoOr even in ES5... [].filter(function(item) { return item != null; }); It's not patched on the prototype, but I'm not sure why something that simple needs to be.