简体   繁体   English

JavaScript - 未捕获的ReferenceError:checkAnswer未定义,函数不在范围内?

[英]JavaScript - Uncaught ReferenceError: checkAnswer is not defined, function not in the scope?

class Pairs extends Screen {
  constructor() {
    super();
    var pair1 = null;
    var nPair1;
    var solution;
    var rightCounter = 0;
    var licao, nS;
  }


  pairScreen(screen, lesson, nScreen) {
    var body = document.body
    var nodes = xmlDoc.getElementsByTagName("PAIRS");
    this.licao = lesson;
    this.nS = nScreen;

    this.solution = screen.getElementsByTagName("SOLUTION")[0].textContent.split(" ");

    body.innerHTML = '';

    Startup.h1(body, "Babel   (" + languageName + ")");
    Startup.hr(body);

    var d = DynamicHTML.div(body, "border:3px solid black; display:table; padding:20px; margin-left:40px");
    Startup.h1(d, "Match the pairs");

    var p1 = Startup.p(d, "padding-left:40px; word-spacing:50px;");
    Startup.text(p1, 16, " ");
    Startup.text(p1, 32, " ");

    var p2 = Startup.p(d, "padding-left:20px;");

    var button;


    var i;
    var f = function(i) {
      Startup.eventHandler2()
    }

    var original = screen.getElementsByTagName("ORIGINAL")[0].textContent;
    var buttons = original.split(" ");


    for (i = 0; i < buttons.length; i++) {
      button = DynamicHTML.inpuButton(p1, i, buttons[i], "orangered");
      Startup.eventHandler2(document.getElementById(i), "onclick", function() {
        checkAnswer(buttons[i], i)
      });
    }
    Startup.hr(body);
  }

  checkAnswer(pair, nPair) {
    var index;
    if (pair1 = null) {
      pair1 = pair;
      nPair1 = nPair;
    } else {
      for (index = 0; index < solution.length; index++) {
        if (pair1 == solution[index]) {
          if (index % 2 == 0 && solution[index - 1] == pair) {
            DynamicHTML.play("general/right_answer.mp3");
            rightCounter = rightCounter + 2;
            document.getElementById(nPair).disabled = true;
            document.getElementById(nPair1).disabled = true;
            pair1 = null;
            nPair1 = null;
          } else if (solution[index + 1] == pair) {
            DynamicHTML.play("general/right_answer.mp3");
            rightCounter = rightCounter + 2;
            document.getElementById(nPair).disabled = true;
            document.getElementById(nPair1).disabled = true;
            pair1 = null;
            nPair1 = null;
          } else {
            DynamicHTML.play("general/wrong_answer.mp3");
            pair1 = null;
            nPair1 = null;
          }

        }
      }
    }
    if (rightCounter == solution.length) {
      if (xmlDoc.getElementsByTagName("LESSON")[licao].childNodes[nS + 2] != null) {
        var fs = new Screen();
        fs.functionScreen(licao, nS + 2);
      } else fs.initialScreen();
    }

  }

}

Wrote this for a JavaScript project I'm working on but when I run it It says Uncaught ReferenceError: checkAnswer is not defined, I'd really appreciate if anyone knew the problem. 为我正在研究的JavaScript项目写了这个,但是当我运行它时它说Uncaught ReferenceError:checkAnswer没有定义,如果有人知道这个问题,我真的很感激。 Thank you! 谢谢!

PS Don't know if the checkAnswer function has bugs or note becausa I couldn't test it out, I will when I can run it :) PS不知道checkAnswer功能是否有错误或注意因为我无法测试它,我会在运行时:)

First you need to bind this at the end of this function: 首先,您需要在此函数的末尾绑定this

Startup.eventHandler2(document.getElementById(i), "onclick", function() {
    this.checkAnswer(buttons[i], i)
}.bind(this));

Basically this tells the anonymous function "hey I want this to refer to this outer class, not the function itself". 基本上这告诉匿名函数“嘿,我希望this引用这个外部类,而不是函数本身”。

(edited) (编辑)的

You need to either bind checkAnswer in the constructor like-so: 你需要在构造函数中绑定checkAnswer ,如下所示:

class Pairs extends Screen {
  constructor() {
    super();
    var pair1 = null;
    var nPair1;
    var solution;
    var rightCounter = 0;
    var licao, nS;
    this.checkAnswer = this.checkAnswer.bind(this);
  }

Or use and arrow function which gives you the exact reference to the this checkAnswer like-so: 或者使用和箭头功能,它给你this checkAnswer的确切参考 - 所以:

checkAnswer = (pair, nPair) => {
    //your code goes here ...
    } 

This way you will be able to call this.checkAnswer inside the scope of your pairScreen function or wherever you want to call the function like @Davelunny point out 通过这种方式,您可以在pairScreen函数的范围内调用this.checkAnswer ,或者像@Davelunny指出的那样调用函数

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

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