简体   繁体   English

Javascript“do-while”类型循环

[英]Javascript “do-while” type loop

I'm working with some JS code that I inherited on a project, and in several instances it has loops set up like this one: 我正在使用我在项目中继承的一些JS代码,并且在几个实例中它具有像这样的循环设置:

while(text = someBufferObject.read()) {
  //do stuff with text
}

I'm assuming this is to achieve some sort of do-while type functionality. 我假设这是为了实现某种类型的do-while类型功能。 However, when I run this through JSLINT it complains that it "Expected a conditional expression and instead saw an assignment." 但是,当我通过JSLINT运行它时,它抱怨它“预期一个条件表达式,而是看到一个赋值。”

Is there a more accepted approach that I should use for these loops? 是否有一种更为公认的方法我应该用于这些循环? I'm not sure if something like below is the best way or not: 我不确定下面的内容是否是最佳方式:

text = someBufferObject.read()
while(text) {
  //do stuff with text
  text = someBufferObject.read()
}

Is there a more accepted approach 是否有更容易接受的方法

Don't take JSLint's advice as gospel. 不要将JSLint的建议视为福音。 It is dogmatic opinion from a cranky old man; 这是一个胡思乱想的老人的教条意见; some of it entirely sensible, some of it rather questionable. 其中一些是完全合理的,有些是相当可疑的。

while (variable= assignment) , though it might sometimes be a mistaken comparator, is also a widely-understood idiom of C-like languages. while (variable= assignment)尽管它有时可能是一个错误的比较器,但它也是一种广泛理解的类C语言的习语。 Whether you use that approach or another is a matter of taste, something you should weigh up personally rather than blindly accept Crockford's edict. 无论你使用这种方法还是其他方法都是品味问题,你应该亲自权衡,而不是盲目地接受克罗克福德的法令。

JavaScript does have a do-while loop, so if your test is consistently at the end that would be a more appropriate construct: JavaScript 确实有一个do-while循环,所以如果你的测试始终在最后,那么这将是一个更合适的构造:

do {
    text= someBufferObject.read();
    // do something
} while (text);

More commonly though what you're looking at is a mid-test loop. 更常见的是,你正在看的是一个中等测试循环。 You may or may not prefer the break idiom as used by Python: 你可能喜欢也可能不喜欢Python使用的break惯用法:

while (true) {
    text= someBufferObject.read();
    if (!text)
        break;
    // do something
}

You only need to wrap it in another set of parentheses to make JSLint happy. 您只需将其包装在另一组括号中以使JSLint满意。

while((text = someBufferObject.read())) {
  //do stuff with text
}

我只能想象这是JSLINT的问题,这是完全有效的javascript,它比第二个解决方案要好得多。

See if JSLINT complains about this: 看看JSLINT是否抱怨这个:

while (NULL != (text = someBufferObject.read())) {
  //do stuff with text
}

Neither example is a "do-while", they're just different code styles that essentially do the same thing. 这两个例子都不是“do-while”,它们只是不同的代码风格,基本上做同样的事情。 JSLint is simply informing you that the first style goes against best practices. JSLint只是告诉你第一种风格违背了最佳做法。

JSLint is complaining because it's a JavaScript code-smell -- using a single-equals (assignment operator) instead of double- or tripe-equals (equality/identity operator) is a common mistake. JSLint抱怨是因为它是一个JavaScript代码 - 气味 - 使用单等号(赋值运算符)而不是double或tripe-equals(相等/身份运算符)是一个常见的错误。

If your code works, don't sweat the warning. 如果您的代码有效,请不要发出警告。 It's an automated tool, not an omniscient tool. 它是一种自动化工具,而不是一种无所不知的工具。

while((text = someBufferObject.read()) !== undefined) {
  //do stuff with text
}

is also accepted by jsLint and a style less cryptical than the paranthese workaround: while((a = b)) { ... } jsLint也接受了一种比paranthese变通方法更少密码的样式:while((a = b)){...}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM