Javascript ES6类-方法无法访问在类构造函数中定义的类属性

时间:2018-12-18 14:36:05

标签: javascript class ecmascript-6

因此,我试图在一个类中构造代码,以使其更有条理,但我很挣扎。我有代码:

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();

我得到了错误:“ main.js:40 Uncaught ReferenceError:未定义ulNavLinks     在HTMLLIElement.clickedLinkState(main.js:40)上。”“ ulNavLinks”是一个nodeList。

例如,我试图使用'this.titleBox = ...'来定义元素,但情况变得更糟,我无法从clickedLinkState方法访问它。在课堂外,它正在工作。

为什么我无法在我的方法中访问“ ulNavLinks”?以及如果我将属性声明为“ this.titleBox”,“ this.navBox”,为什么无法在方法内访问属性?

2 个答案:

答案 0 :(得分:0)

到目前为止,在JavaScript中,只能使用关键字thishere is the doc)在类方法中定义实例属性。

还有experimental feature支持公共/私有字段,由于浏览器支持不佳,您可能会在一些构建步骤中使用它。

答案 1 :(得分:0)

确保使用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();