如何在函数中的类中调用类函数

时间:2021-04-09 12:27:12

标签: javascript class

我想调用 handleopenfirst() 函数内的 handlecloseall()init() 函数。

如果我这样做,我会收到错误:

Uncaught TypeError: this.handlecloseall is not a function at HTMLDivElement.<anonymous>

谢谢,

这里是JS代码:

class MLMenu {
    constructor(menuEL){
        this.MenuEl = menuEl;
        this.buttonopenclose = this.MenuEl.querySelector("#buttonopenandclose");
        this.navigationopen = false;
        console.log(this.MenuEl)
        console.log(this.buttonopenclose)
        this.init();
        
    }

    

    init(){
        //add Eventlistener to Open and Close Button
        var button = this.buttonopenclose;
        button.addEventListener('click', function(){
            if (this.navigationopen === false) {
                this.navigationopen = true;
                button.classList.add('open');
                button.classList.remove('close');
                
                this.handleopenfirst();

            

                console.log("Navigation should be opend now");
            } else {
                this.navigationopen = false;
                button.classList.add('close');
                button.classList.remove('open');

                this.handlecloseall();

                console.log("Navigation should be closed now");
            }
        
        });
    }

    handleopenfirst(){
        console.log('hallo');
    }

    handlecloseall(){
        console.log('hallo');
    }
    
}

0 个答案:

没有答案
相关问题