Skip to content Skip to sidebar Skip to footer

Why Do I Get Log Is Not Defined

I am new to JS and was learning classes in JS but I faced an error saying log is not defined. Here is the code:

Solution 1:

You want this.log(). I added the extra console log so you can see the output properly.

classKeyboard {
  log() {
    returntrue;
  }

  print() {
    console.log(this.log() ? "True" : "False");
  }
}

const mir = newKeyboard();
mir.print();

Solution 2:

You have defined log() as part of class Keyboard so it is not available in the gobal namespace. You have to access it through a Keyboard object. If you are accessing it from within another Keyboard function, you can use this

classKeyboard {
  log() {
    returntrue;
  }

  print() {
    this.log() ? "True" : "False";
  }
}

const mir = newKeyboard();
mir.print();

Post a Comment for "Why Do I Get Log Is Not Defined"