Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

307
Visualizações
TypeError: Cannot read properties of undefined (reading 'a')

I was trying classes and the this keyword in Javascript,and I wrote this code:

class p{
  constructor(p,a){
      p = p.p;
      a = p.a;
  };
};
const c = new p(p="1st aspect",a="2nd aspect");
console.log(this.c);

And I got this Error:

Cannot read properties of undefined (reading 'a')

I don't know whether this is compatible with classes or whether I passed in the wrong arguments,can someone help me there?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

The syntax for creating a class is wrong. You don't need to pass the parameter names. Just use

const c = new p("1st aspect","2nd aspect");

And it is not good practice to use lowercased frist letter as class name. Use P instead of p. And this.c is wrong. Just print

console.log(c);
about 4 years ago · Juan Pablo Isaza Relatório

0

It's just the syntax you're using to create a class instance isn't correct in terms of attempting to use provided argument names ("p =", "a ="), and there may be some errors in your class construction. I think you may have been trying to do something like this:

class p {
  // These are ordered arguments, not named arguments 
  constructor(p,a) {
      // Save the value of p and a on the class instance
      this.p = p;
      this.a = a;
  };
};
// Create a new p class instance with the arguments in order
const classInstance = new p("1st aspect", "2nd aspect");
about 4 years ago · Juan Pablo Isaza Relatório

0

Code should look like this

class P {
  constructor(p, a){
      this.p = p;
      this.a = a;
  };
};


const c = new P("1st aspect", "2nd aspect");
console.log(c); // <- just print c

I can see you're mixing some concepts and getting confused, I'd suggest you to read official MDN Docs, you'll learn about classes step by step.

As an extra, you'll notice that class names are named using PascalCase. This is because by following the naming convention you'll read and understand other people's code much faster (and even yours).

Hope this helped!

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda