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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

ES7 Array and Generator comprehensions

38 点作者 mdibaiee将近 10 年前

4 条评论

zaius将近 10 年前
While I really like comprehensions, as soon as you need a conditional, or any level of nesting, you pretty much have to break it out into a loop to keep it readable.<p>OTOH, block syntax (I don&#x27;t think that&#x27;s the correct term for it) has always irked me in JS, due to verbose function declaration, and no implicit return. So beautiful syntax in ruby like:<p><pre><code> some_array.map { |el| el.length } # Or the succinct version: some_array.map(&amp;:length) </code></pre> Looks like this in JS:<p><pre><code> someArray.map(function() { return this.length; }); </code></pre> Now that ES6 has arrow functions with implicit returns, we can do this:<p><pre><code> someArray.map( s =&gt; s.length ); </code></pre> Since both comprehensions and implicit return functions are being released around the same time, I&#x27;ll be interested to see which of the two gets more adoption.
评论 #9683063 未加载
aidos将近 10 年前
Nice to see generator comprehensions in js. Glad that I don&#x27;t have to remember anything much when coming from Python due to the choice of &#x27;(&#x27;.<p>Does anyone know where the for..of syntax comes from? Just seems to deviate from the way that other languages represent comprehensions (including maths).
评论 #9683956 未加载
colanderman将近 10 年前
This is bizarre to me. Why complicate the browser with a feature that amounts to syntactic sugar?<p>Why not keep JS simple (or better yet, simplify it further!) so that browser implementations might finally converge, and leave the syntactic niceties to preprocessors &amp; compilers?
评论 #9683802 未加载
taco_emoji将近 10 年前
I read about stuff like this and then wonder whether I&#x27;ll ever be able to use it since target browser platforms are so fragmented.
评论 #9682824 未加载