开发者

How do you write an empty while loop in Coffeescript?

I'm trying to translate some old code to Coffeescript. But there is no direct translation for:开发者_运维技巧

while ( doWork() ) {}

"while doWork()" with nothing after it results in a syntax error.


while doWork() then

Should do the trick


using then is probably the canonical solution since it is explicitly meant for separating the condition from the (in this case empty) body. Alternatively you can write

while doWork()
  ;#

(the # keeps vim syntax highlighting from flagging it as an error)

I also like the continue while doWork() solution, but I strongly advise against any other form of expression while doWork() mentioned in the comments since when this is the last statement of a function it will become a list constructor:

_results = [];
while (doWork()) {
  _results.push(expression);
}
return _results;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜