0

I am trying to learn to use class syntax in my javascript. This is working until the click eventlistener is activated. When it reaches the "toggle" method the "_body" property doesn't seem to available anymore on "this". I am sure this is a scope problem but i cannot see whats wrong.

I get this error message:

navigation.js:13 Uncaught TypeError: Cannot read properties of undefined (reading 'classList')
    at HTMLButtonElement.toggle 




export default class Navigation {
    
    _menuBtn    =  document.getElementById("primary-nav-menu");
    _body       =  document.getElementsByTagName("body")[0];

    constructor() {
        this._menuBtn.addEventListener("click", this.toggle);
    }
    
    toggle() {
        this._body.classList.toggle("primary-nav_open");
    }
}
8bitdane
  • 1
  • 2

0 Answers0