简体   繁体   中英

Javascript ES6 Classes - Method cant access class property defined inside class constructor

So i was trying to structure my code inside a class so it can be more organized, but iam struggling. I have the code:

class App {

  constructor() {

    // Get elements from DOM
    const titleBox = document.getElementById('titleBox');
    const navBox = document.getElementById('navBox');
    const navLinks = document.querySelectorAll('.header__listLink');
    const headerTitle = document.getElementById('headerTitle');
    const headerSubtitle = document.getElementById('headerSubtitle');
    const ulNav = document.getElementById('ulNav');
    const ulNavLinks = document.querySelectorAll('.ulNavLink');

    // for each nav link, add an event listener, expanding content area
    navLinks.forEach((link) => {
      link.addEventListener('click', this.clickedLinkState);
    });

  }

  clickedLinkState(e) {

      e.preventDefault();

      titleBox.classList.remove("header__title-box");
      titleBox.classList.add("header__title-box--linkClicked");

      headerTitle.classList.remove("header__title");
      headerTitle.classList.add("header__title--linkClicked");

      headerSubtitle.classList.remove("header__subtitle");
      headerSubtitle.classList.add("header__subtitle--linkClicked");

      ulNav.classList.remove("header__listInline");
      ulNav.classList.add("header__listInline--linkClicked");

      navBox.classList.remove("header__nav-box");
      navBox.classList.add("header__nav-box--linkClicked");

      ulNavLinks.forEach((navLink) => {
        navLink.classList.remove("header__listLink");
        navLink.classList.add("header__listLink--linkClicked");
      });   

  }

}

const app = new App();

And i got the error: "main.js:40 Uncaught ReferenceError: ulNavLinks is not defined at HTMLLIElement.clickedLinkState (main.js:40)". the 'ulNavLinks' is a nodeList.

I was trying to define the elements using 'this.titleBox = ...', for exemple, but it got even worse, i could not access it from my clickedLinkState method. Outside the class it was working.

Why i cant access the 'ulNavLinks' inside my method? and why i cant access my propesties inside the method if i declare them 'this.titleBox', 'this.navBox'?

In JavaScript, as for now, instance properties can only being defined inside class methods using keyword this ( here is the doc ).

Also there is an experimental feature of supporting public/private fields, which you may use with some build steps due to poor browser support.

Make sure to use this :

 class App { constructor() { // Get elements from DOM this.titleBox = document.getElementById('titleBox'); this.navBox = document.getElementById('navBox'); this.navLinks = document.querySelectorAll('.header__listLink'); this.headerTitle = document.getElementById('headerTitle'); this.headerSubtitle = document.getElementById('headerSubtitle'); this.ulNav = document.getElementById('ulNav'); this.ulNavLinks = document.querySelectorAll('.ulNavLink'); // for each nav link, add an event listener, expanding content area this.navLinks.forEach((link) => { link.addEventListener('click', this.clickedLinkState.bind(this)); }); } clickedLinkState(e) { e.preventDefault(); this.titleBox.classList.remove("header__title-box"); this.titleBox.classList.add("header__title-box--linkClicked"); this.headerTitle.classList.remove("header__title"); this.headerTitle.classList.add("header__title--linkClicked"); this.headerSubtitle.classList.remove("header__subtitle"); this.headerSubtitle.classList.add("header__subtitle--linkClicked"); this.ulNav.classList.remove("header__listInline"); this.ulNav.classList.add("header__listInline--linkClicked"); this.navBox.classList.remove("header__nav-box"); this.navBox.classList.add("header__nav-box--linkClicked"); this.ulNavLinks.forEach((navLink) => { navLink.classList.remove("header__listLink"); navLink.classList.add("header__listLink--linkClicked"); }); } } const app = new App(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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