3 ms·
> ["1","2","3"].map(function(value) {return parseInt(value)}) > (…) it is much more verbose and arguably less elegant. Article is from 2011 and referencing ES
by latexr 3y ago
> ["1","2","3"].map(function(value) {return parseInt(value)})
> (…) it is much more verbose and arguably less elegant.
Article is from 2011 and referencing ES5. With ES6 arrow functions we can make it shorter and more elegant:
["1","2","3"].map(value => parseInt(value))
Because it’s so short, even `n` should suffice and retain clarity:
["1","2","3"].map(n => parseInt(n))
- flockonus 3y agoThis... I prefer 10x more obvious code than elegant code. Not to mention, altering primitives class prototypes can lead to unexpected side effects.
- ccooffee 3y agoSure, but to update their example using the new format, you'd say: ["1","2","3"].map(parseInt); // returns [1, NaN, NaN] The gotcha trap is still there, and is just as likely to get through a code review.
- latexr 3y agoYes, you still need to know that you have to do this, but my point is that the author started with the quoted alternative and abandoned it because it had two problems (verbose, inelegant) which are eliminated in ES6, making the `Function.prototype.only` solution unnecessary.
- mattlondon 3y agoYep came here to say this. Arrow functions are graceful and preferred precisely because of this. Passing a naked function reference like that to map is frowned upon where I work, and your PR would not get approval because it is risky for the reasons pointed out in this article. TL;DR - use modern JavaScript features to make your code more readable and more reliable.