Iterate over ES6 Set/Map in Coffeescript (with `of` operator)(在 Coffeescript 中迭代 ES6 Set/Map(使用 `of` 运算符))
问题描述
如何迭代 ES6 Map 或 在Coffeescript中设置?
在 Javascript 中可以使用例如
s = new Set()s.add({a: 1})对于 (x of s) {控制台.log(x);}
但是 Coffeescript 有自己的 of
运算符,可以转换为 in
,即:
console.log(x) for x of s
变成 ... for (x in s) { ... }
.
如何在 Coffeescript 中访问 Javascript 的 of
运算符?
可以通过循环s.values().next()
来编写自己的自定义迭代器,但那将是可憎的.:)
Coffeescript 2.0 —for …from
<块引用>JavaScript 的 for...of 现在可以作为 CoffeeScript 的 for ...from
使用(我们已经有一个 for ...of
): for n from generatorFunction()代码>
Coffeescript 1.0 - 反引号
一种选择是使用反引号嵌入原始 Javascript,即 http://coffeescript.org/#embedded.
`for (x of y) { }`
How can one iterate over an ES6 Map or Set in Coffeescript?
In Javascript one would use e.g.
s = new Set()
s.add({a: 1})
for (x of s) {
console.log(x);
}
However Coffeescript has its own of
operator that gets converted to in
, i.e.:
console.log(x) for x of s
becomes ... for (x in s) { ... }
.
How can one access Javascript's of
operator in Coffeescript?
One could write their own custom iterator by cycling over s.values().next()
, but that'd be an abomination. :)
Coffeescript 2.0 — for …from
JavaScript’s for…of is now available as CoffeeScript’s
for …from
(we already had afor …of
):for n from generatorFunction()
Coffeescript 1.0 — Backticks
One option is to use the backtick to embed raw Javascript i.e. http://coffeescript.org/#embedded.
`for (x of y) { }`
这篇关于在 Coffeescript 中迭代 ES6 Set/Map(使用 `of` 运算符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Coffeescript 中迭代 ES6 Set/Map(使用 `of` 运算符)


基础教程推荐
- Bootstrap 模态出现在背景下 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01