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

195
Visualizações
this.init not a function

I'm doing this for class and the test they have says my constructor is not being initialized so I looked that up and found this.init(). Now I am getting "this.init() is not a function and I am not sure why.

My code:

function CuboidMaker(length, width, height){
  const volume = () => {
    return this.length * this.width * this.height
  }

  const surfaceArea = () => {
    return 2 * (this.length * this.width + this.length * this.height + this.width * this.height)
  }

  this.length = length
  this.width = width
  this.height = height
  this.volume = volume
  this.surfaceArea = surfaceArea
  this.init()
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Is it simply that you're not creating a new instance of the constructor?

Also, volume and surfaceArea are both functions and need to be called.

function CuboidMaker(length, width, height){

  const volume = () => {
    return this.length * this.width * this.height;
  }

  const surfaceArea = () => {
    return 2 * (this.length * this.width + this.length * this.height + this.width * this.height);
  }

  this.length = length;
  this.width = width;
  this.height = height;
  this.volume = volume();
  this.surfaceArea = surfaceArea();

}

const a = new CuboidMaker(1, 2, 3);
console.log(a);

about 4 years ago · Juan Pablo Isaza Relatório

0

I think you don't initialize you're class correctly. Try this syntax :

class CuboidMaker{
        constructor(length, width, height){
            this.length = length;
            this.width = width;
            this.height = height;
        }
        volume(){
          return this.length * this.width * this.height
        }
        surfaceArea(){
                return 2 * (this.length * this.width + this.length * this.height + this.width * this.height)
        }
    }
    
    let myCuboid = new CuboidMaker(1, 2, 3);
    console.log(myCuboid.volume());
    console.log(myCuboid.surfaceArea());

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