Coffeescript 中的循环与操作
如何将此循环转换为咖啡脚本?
for (var j = world.m_jointList; j; j = j.m_next) {
drawJoint(j, context);
}
How can I convert this loop to coffeescript?
for (var j = world.m_jointList; j; j = j.m_next) {
drawJoint(j, context);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
但一般来说,仅将初始化代码作为单独的语句编写
for
循环有什么问题,例如j=first;而 j 那么...; j=j.next
?编辑:
或者,可能:
But generally, what's wrong with just writing
for
loops with the initialize code as a separate statement, e.g.j=first; while j then ...; j=j.next
?edit:
Or, possibly:
原文中发生了什么:
Javascript 有 复合 for 循环。括号内的声明具有三个表达式:
for(x; y; z){...}
。x
在循环之前运行一次y
是一个条件,在每次迭代之前进行测试。如果为 false,循环将z
运行一次。在此代码中,您设置
j = world.m_jointList
,它是链接列表中的第一项。for
循环的中间部分正在检查真实性 ofj;
,并且在每次迭代之后,j
被设置为j.m_next
,它是指向链中下一个对象的指针。当j
计算结果为 false(在本例中可能是undefined
)时,它结束。为了形象化,
world
可能看起来像这样:实际上,列表中的项目可能不作为
world
的属性存在,但这会起到相同的作用。请注意,最后一个对象中的m_next
未定义。有时,尾部的占位符值将用于指示链的末尾。名称
m_jointList
在这里也有点误导,因为它实际上并不包含列表,只包含列表的第一个元素。这应该在 CoffeeScript 中完成:
这将是 javascript 中
do...while
的一个很好的用途:What's happening in the original:
Javascript has compound for loops. The declaration inside the parenthesis has three expressions:
for(x; y; z){...}
.x
runs once before the loopy
is a condition, tested before each iteration. If it's false the loop will stopz
runs once after every iterationIn this code, you set
j = world.m_jointList
, which is the first item in the linked list. The middle part of thefor
loop is checking for thruthiness ofj;
, and after each iterationj
is set toj.m_next
, which is a pointer to the next object in the chain. It ends whenj
evaluates to false (probablyundefined
in this case).To visualize that,
world
could look like this:In reality the items in the list probably don't exist as properties of
world
, but this would work the same. Notice thatm_next
is undefined in the last object. Sometimes a placeholder value for the tail will be used indicating the end of the chain.The name
m_jointList
is also a bit misleading here because it doesn't actually contain the list, just the first element of it.This should do it in coffeescript:
And that would've been a good use of
do...while
in javascript:这是最好的:
它只是描述了 for 循环的含义。
this is the best:
it just describes what the
for
loop mean.