-1

Can anyone tell me why the Class Property values don't appear in Console when i execute this code? Many thanks.

class Pessoa
{
  constructor(nome, idade)
  {
    this.nome = nome;
    this.idade = idade;
  }

  Info()
  {
    console.log('O meu nome é ${this.nome} e tenho ${this.idade} anos.');
  }
}

p1 = new Pessoa("Pedro", 49);
p1.Info();
Pedro Figueiredo
  • 457
  • 5
  • 13

1 Answers1

1

In order to use the ${expression} template string syntax you must use the ` character, not the '.

class Pessoa
{
  constructor(nome, idade)
  {
    this.nome = nome;
    this.idade = idade;
  }

  Info()
  {
    console.log(`O meu nome é ${this.nome} e tenho ${this.idade} anos.`);
  }
}

p1 = new Pessoa("Pedro", 49);
p1.Info();
Gunther
  • 1,297
  • 9
  • 15