繁体   English   中英

如何使用数组创建新对象而不立即调用它们?

[英]How can I use an array to create new objects without calling them immediately?

我是编程新手,我正在开发一个小型问答游戏。 我正在使用构造函数来创建 object 来表示问题蓝图。 我希望 object 保留问题的“解决方案”和“可能的答案”。 我保存在一组对象中的可能答案。 该数组包含所有新对象,并且在控制台中测试时效果很好。 (但在运行脚本时无法正常工作)。

所以问题是,在我的阵列中,我创建了一个新的 object 但它立即被调用到控制台。 我不希望数组自动打印到控制台。 因为我需要这个来玩游戏,所以我只想按请求的索引打印问题。 但它正在将整个数组打印到控制台而不被调用。

(这可能与执行堆栈有关,但我需要帮助来解决这个问题。我对编程很陌生,所以请详细说明)。

var questionList = [
new Question('is learning hard?', ['yes', 'no'], 'no'),
new Question('Is JavaScript a fun programming language?', ['yes','no'], 'yes'),];

var pickQuestion = Math.floor(Math.random() * 2);
console.log(questionList[pickQuestion].ask);


//Question class (below):

var Question = function(question, answers, correct_answer) {
  this.question = question;
  this.answers = answers;
  this.correct_answer = correct_answer;
  ask: {
    console.log(this.question);
    for(var i = 0; i < answers.length; i++){
      console.log(i + ": " + answers[i]);
    }
  }
}

这是因为ask属性未正确定义为 function。

var Question = function(question, answers, correct_answer) {
  this.question = question;
  this.answers = answers;
  this.correct_answer = correct_answer;
  this.ask = () => {    // note the brackets & arrow, could also be function()
    console.log(this.question);
    for(var i = 0; i < answers.length; i++){
      console.log(i + ": " + answers[i]);
    }
  }
}

您的 class 定义需要一些工作。 要添加方法,标准方法是将其添加到构造函数的原型,例如Question.prototype.ask = function() {...}或使用新的 JS class语法将其定义为方法(这仍将其添加到原型)。 否则,如果您在构造函数中创建一个新的 function ,您将为每个Question创建一个新的 function 。 这会起作用,但这不是一个好的做法(在大多数情况下),因为您只需要一个 function。 以下代码片段应该大部分都可以按预期工作,并且比在构造函数中设置 function 更好:

“旧” JS(使用prototype ):

 function Question(question, correctAnswer, answers) { this.question = question; this.answers = answers; this.correctAnswer = correctAnswer; } Question.prototype.ask = function() { console.log(this.question); for (let i = 0; i < this.answers.length; i++) { console.log(i + ": " + this.answers[i]); } }; function pickQuestion(q) { return q[ Math.floor(Math.random() * q.length) ]; } var question = pickQuestion([ new Question('Choose Answer 1', 1, [ 'Answer 1', 'Answer 2', 'Answer 3' ]), new Question('Choose Answer 3', 3, [ 'Answer 1', 'Answer 2', 'Answer 3', ]), new Question('Choose Answer 2', 2, [ 'Answer 1', 'Answer 2', 'Answer 3', ]) ]); question.ask();

“新”JS(使用class等功能):

 class Question { constructor(question, correctAnswer, answers) { this.question = question this.answers = answers this.correctAnswer = correctAnswer } ask() { console.log(this.question) for (let i = 0; i < this.answers.length; i++) { console.log(i + ": " + this.answers[i]) } } } const pickQuestion = q => q[ Math.floor(Math.random() * q.length) ] const question = pickQuestion([ new Question('Choose Answer 1', 1, [ 'Answer 1', 'Answer 2', 'Answer 3' ]), new Question('Choose Answer 3', 3, [ 'Answer 1', 'Answer 2', 'Answer 3', ]), new Question('Choose Answer 2', 2, [ 'Answer 1', 'Answer 2', 'Answer 3', ]), ]) question.ask()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM