因此,出於某種原因,當我在此代碼中使用增量運算符時,它不起作用。我已經驗證了我的變量是數字。不知道發生了什麼事。JavaScript增量運算符不工作?
var fs = require('fs')
, bt = require('buffertools')
var start = 0;
fs.readFile(process.argv[2], function(err, data) {
console.log(data);
while (bt.indexOf(data, '\n', start)) {
var nl = bt.indexOf(data, '\n', start); // nl is 40
console.log(start, nl); // 0, 40
console.log(data.slice(start, nl)); // works great!
start = nl++; // reset the offset past that last character..
console.log(start, typeof start); // start == 40? the heck? 'number'
process.exit(); // testing
//console.log(nl, start); 40, 40
}
});
EDIT ------
而且解決方案......
"use strict";
var fs = require('fs')
, bt = require('buffertools');
fs.readFile(process.argv[2], function(err, data) {
var offset = 0;
while (true) {
var nl = bt.indexOf(data, '\n', offset);
if (nl === -1) break;
console.log(data.slice(offset, nl));
offset = ++nl;
}
console.log(data.slice(offset));
});
謝謝!
'nl ++'。這樣做是增加'nl'並返回*原始*值。試試'start = ++ nl'。 '++ nl'會增加'nl'並返回* new *值。 –