查看来自:https://github.com/jprichardson/node-fs-extra#walk 的“Streams 2 & 3(拉)示例”
var items = [] // files, directories, symlinks, etc
var fs = require('fs-extra')
fs.walk(TEST_DIR)
.on('readable', function () {
var item
while ((item = this.read())) {
items.push(item.path)
}
})
.on('end', function () {
console.dir(items) // => [ ... array of files]
})
最新版本的 JSLint 关于 while
的提示:
Unexpected statement '=' in expression position.
while ((item = this.read())) {
Unexpected 'this'.
while ((item = this.read())) {
我正在尝试弄清楚如何以 JSLint 认可的方式编写它。有什么建议吗?
(注意:我知道这段代码中还有其他 JSLint 违规行为……我知道如何解决这些问题……)
最佳答案
如果你真的像 Douglas Crockford(JSLint 的作者)一样对编写这段代码感兴趣,你会使用递归而不是 while
循环,因为在 ES6 中有尾调用优化。
var items = [];
var fs = require("fs-extra");
var files = fs.walk(TEST_DIR);
files.on("readable", function readPaths() {
var item = files.read();
if (item) {
items.push(item.path);
readPaths();
}
}).on("end", function () {
console.dir(items);
});
关于javascript - 如何以 JSLint 认可的方式重写这个 while 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41371041/