简体   繁体   English

变量在一个 function 中定义,在另一个 typescript 中未定义

[英]variable defined in one function and undefined in other typescript

I have the following code:我有以下代码:

class Currency {
    private counter = document.getElementById('counter');
    private dust = 0;
    private books = 0;
    private bookCounter = document.getElementById("books");
    
    constructor() {
        console.log("test")
        document.getElementById("bookbuy").addEventListener("click", this.buyBook)
    }

    public count() {
        this.dust ++;
        this.counter.innerHTML = "You have " + this.dust + " dust";
        console.log(this.dust)
    }

    public buyBook() {
        if (this.dust >= 10) {
            console.log("if works");
            this.dust - 10;
            this.books ++;
            this.counter.innerHTML = "You have " + this.dust + " dust";
            this.bookCounter.innerHTML = "You have " + this.books + " books";
        } else {
            console.log(this.dust)
        }
    }
  }

window.addEventListener("load", init);
function init(): void {
    const currency = new Currency();
    setInterval(() => {currency.count();}, 1000);
} 

the console.log in count() is defined and working fine, but when i try to use this.dust in buyBook() it returns as undefined. count() 中的 console.log 已定义并且工作正常,但是当我尝试在 buyBook() 中使用 this.dust 时,它返回未定义。 Why is this and how do I fix it?为什么会这样,我该如何解决?

You want to bind the this context for buyBook, or the event handler will redefine the this context.您想为 buyBook 绑定 this 上下文,否则事件处理程序将重新定义 this 上下文。

Edit: Also, I think you meant to decrement 10 from this.dust not just subtract 10编辑:另外,我认为你的意思是从this.dust中减去 10 而不仅仅是减去 10

class Currency {
    private counter = document.getElementById('counter');
    private dust = 0;
    private books = 0;
    private bookCounter = document.getElementById("books");
    
    constructor() {
        console.log("test")
        document.getElementById("bookbuy").addEventListener("click", this.buyBook.bind(this))
    }

    public count() {
        this.dust ++;
        this.counter.innerHTML = "You have " + this.dust + " dust";
        console.log(this.dust)
    }

    public buyBook() {
        if (this.dust >= 10) {
            console.log("if works");
            this.dust -= 10;
            this.books ++;
            this.counter.innerHTML = "You have " + this.dust + " dust";
            this.bookCounter.innerHTML = "You have " + this.books + " books";
        } else {
            console.log(this.dust)
        }
    }
  }

window.addEventListener("load", init);
function init(): void {
    const currency = new Currency();
    setInterval(() => {currency.count();}, 1000);
} 

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

相关问题 Typescript:变量可能在匿名 function 内部未定义 - Typescript: variable possibly undefined inside anonymous function javascript:未定义变量与未定义函数 - javascript: variable defined vs function undefined Angular 2-打字稿| 变量不能在其他功能中使用 - Angular 2 - Typescript | variable is not usable in other function 如果一个变量未定义,则将新变量设置为已定义的变量,而不使用“if”语句? - If a variable is undefined set new variable to a defined one, without 'if' statement? 检查变量是否在 Typescript 中定义? - Check if variable is defined in Typescript? 在函数外部定义的javascript变量在函数内是“未定义的” - A javascript variable defined outside a function is 'undefined' within the function 具有未定义参数且参数在函数体中定义的Javascript函数,是否为全局变量? - Javascript function with undefined parameter and the parameter is defined in function body , is a global variable? 在函数外部调用时,在函数中定义的全局变量显示未定义 - Variable global defined in a function is showing undefined when called outside the function Typescript 将定义的值推断为未定义 - Typescript inferring defined value as undefined 一个.env 变量正确返回,但另一个在同一文件中未定义 - One .env variable returns correctly but the other one is undefined in the same file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM