[英]How can I rewrite this while loop in a JSLint-approved way?
查看來自以下網址的“ Streams 2和3(拉)示例”: https : //github.com/jprichardson/node-fs-extra#walk
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);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.