我在尝试定义构造函数的第三个属性时遇到了麻烦。当我评论this.partial = text.replace(cloze,'...');
时它运行正常。
但是当我使用那行代码时,程序结束了中间查询器。我认为它与.replace()
正则表达式有关。
这是我的代码:
var fs = require("fs");
var action = process.argv[3];
var count = process.argv[4];
function ClozeCard(text, cloze) {
this.text = text;
this.cloze = cloze;
this.partial = text.replace(cloze,'...');
}
var inquirer = require('inquirer');
var cards = [];
var num = 0;
var start = {
createCards: function() {
if (num < count) {
inquirer.prompt([
{
name: "text",
message: "What is the Cloze-flashcard's text?"
}, {
name: "cloze",
message: "What is the Cloze-flashcard's answer?"
}
]).then(function (answers) {
var newCard = new ClozeCard(answers.text, answers.cloze);
if (answers.text.indexOf(answers.cloze) === -1) {
console.log("The answer does not appear to be in the text. Try again");
return;
} else {
cards.push(newCard);
num++;
fs.writeFile('cloze.txt', JSON.stringify(cards), function (err) {
if (err) {
return console.log(err);
}
});
start.createCards();
}
});
}
}
}
start.createCards();
module.exports = ClozeCard;