TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

You Might Not Need Underscore

8 点作者 ville将近 10 年前

8 条评论

haromaster将近 10 年前
There is some really good points here. However, i feel a lot of these 'You might not need...' articles are often a straw man response to the idea that developers only use libraries because they are easier to code, not because (some of them) solve cross-browser issues or provide better performance, offer features that there isn't a 1:1 native method for etc.
评论 #9840084 未加载
maroshii将近 10 年前
One of the advantages of underscore (and lodash) is that you can iterate over arrays AND objects indistinctly in most cases. How would you do:<p><pre><code> var a1b2 = _.reduceRight({a:1,b:2},(memo,val,key) =&gt; key+val+memo,&#x27;&#x27;); </code></pre> or:<p><pre><code> var two = _.find({a:2,b:3,c:4},(val,key) =&gt; key !== &#x27;a&#x27; &amp;&amp; key !== &#x27;c&#x27; ) </code></pre> Until I see _.chain, _.compose, _.partialRight, _.zip or _.throttle (among others) implemented in the standard library lodash is a no-opt.<p>With iterables and the &quot;for of&quot; loop javascript is definitely not going in the direction of functional programming though.
LocalPCGuy将近 10 年前
The article doesn&#x27;t touch at all on the performance of lodash compared to underscore or even native functions. In many cases it is more performant. So while you do have to decide if the extra library load is balance by the better performance in your specific use case, it should be considered.<p>Also, there is the simple fact a lot of people like the abstraction compared to the native solution - it is often simpler to use and remember.
评论 #9869347 未加载
评论 #9838546 未加载
togakangaroo将近 10 年前
Interesting post with a few neat things I didn&#x27;t know (spread on an object? Very cool)<p>But then you still have. To deal with awesome functional helpers like denounce, not to mention insanely useful things like cloneDeep and zipObject. Instead I&#x27;d note to use the natives tuff wherever possible and use lodash to pull in just the functions you need piecemeal.<p>Oh and you should certainly mention Function.prototype.reduce and Object.assign
fabien_将近 10 年前
Underscore : _.each(array, iteratee) ES5.1 : array.forEach(iteratee)<p>And all the other elements...<p>Yes, you are right. But... The &quot;array&quot; is not always an Array. document.getElementsByClassName( &#x27;myClass&#x27; ) is not an Array but a DOMNodeList. So document.getElementsByClassName( &#x27;myClass&#x27; ).forEach(iteratee) will not work. Same for map, reduce...<p>Underscore (or lodash) makes more that just polyfilling es5 methods.
评论 #9864544 未加载
评论 #9872243 未加载
GreLI将近 10 年前
What about _.flatten()? E.g. to flatten an array before find()?<p>Just thought of 1-level flattening [].concat(...array). But it not that explicit. Also, array.find(i =&gt; i.foo == &#x27;bar&#x27;) isn&#x27;t as readable as _.find(array, { foo: &#x27;bar&#x27; }).
inglor将近 10 年前
`Array.from({ length: n }, (v, k) =&gt; k + x)` this is a terrible way to create a range, it only works incidentally. Please do not use it.
评论 #9826309 未加载
mc_hammer将近 10 年前
true but underscore had the features first... so, i might not need es2015. also i have no idea how to install es2015 or the browser compatibility it brings.<p>thx for the info though. :)
评论 #9794831 未加载