我发现文档非常混乱。我什么时候应该使用for x of y
,for x from y
和for x in y
,普通的Javascript会翻译成什么?有什么选择?
答案 0 :(得分:0)
# Coffeescript # Translates to Javascript
# Iterables like Arrays and Generators:
for e from arr # for (e of arr)
# Arrays: "Comprehension"
for e in arr # for (j = 0, len = arr.length; j < len; j++) {
# Arrays, functionally:
arr.forEach (a) => # arr.forEach a =>
# Objects: "Comprehension"
for k,v of obj # for (k in obj) {
# v = obj[k]
# Objects, only where hasOwnProperty
for own k,v of obj # for (k in obj) {
# if obj.hasProperty(k)
# Or manually, where hasOwnProperty
for [k,v] from Object.entries(obj) # for (x of obj) {
# [k, v] = x;
# Or manually, functionally, where hasOwnProperty
Object.entries(obj) # Object.entries(obj)
.forEach ([k,v]) => # .forEach(([k, v]) =>
# As embedded Javascript
`for (e of arr)` # for (e of arr)
`for (k in obj)` # for (k in obj)
简而言之:
from
成为of
in
变成复杂的for(;;)
循环,并且of
成为in
。