我怎样才能以JSLint批准的方式重写这个循环?
看看“Streams 2&3(pull)example”from: 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最新版本的投诉:
Unexpected statement '=' in expression position. while ((item = this.read())) { Unexpected 'this'. while ((item = this.read())) {
我试图找出如何以JSLint批准的方式写这个。 有什么build议么?
(注意:我知道这个代码中还有其他的JSLint违规行为…我知道如何解决这些…)
如果您真的有兴趣编写像Douglas Crockford(JSLint的作者)这样的代码,那么您将使用recursion而不是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); });