首页 > 解决方案 > 如何在 if / else 条件下使用 JavaScript 构造函数类变量?

问题描述

我在控制台中收到一条错误消息,“this.cardPlace”和“this.flipPlace”都未定义。我在 if/else 条件之前运行了一个 console.log,它们都运行正常。我可能缺少一些小东西。(我对编码很陌生,所以请不要给我扔石头,哈哈!)谢谢你的帮助!

class CardSection {
    constructor(_cardPlace, _flipPlace, _cat_section, _flipButton, _min, _max) {
        this.cardPlace = _cardPlace;
        this.flipPlace = _flipPlace;
        this.flippy = function() {
            if (_flipButton.value == "side-one") {
                this.flipPlace++;
                _flipButton.value = "side-two";
                document.getElementsByClassName("location").innerHTML = "Answer Side";
                _cat_section.src = "cards/" + this.cardPlace + "-" + this.flipPlace + ".tiff";
            } else if (_flipButton.value == "side-two") {
                this.flipPlace--;
                _flipButton.value = "side-one";
                document.getElementsByClassName("location").innerHTML = "Question Side";
                _cat_section.src = "cards/" + this.cardPlace + "-" + this.flipPlace + ".tiff";
            }
        }; 

...

var cat_section = document.getElementById("catechism-section-1");
var flipButton = document.getElementById("flip-button1");

var sect1 = new CardSection(1, 1, cat_section, flipButton, 1, 9);

在此处输入图像描述

标签: javascriptif-statementconstructor

解决方案


推荐阅读