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

171
Visualizações
Are there any differences between Class and Object Functions defining methods?

Are the two defining methods pretty much the same? Is there anything I should watch out for?

// A.
function Dog(name, color, numLegs) {
  this.name = name;
  this.color = color;
  this.numLegs = numLegs;
}

Dog.prototype.greet = function() {
  return 'Bark!';
}
// B.
class Dog {
  constructor(name = '', color = '', numLegs = 0) {
    this.name = name;
    this.color = color;
    this.numLegs = numLegs;
  }

  greet() {
    return 'Bark!';
  }
}

I'm refactoring an old application and I'm wondering if it's safe to restructure all existing object functions from (A) to (B) as shown above.

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

0

A js class is a beautified way of writing your first bit of code. So yes these two syntaxes do exactly the same thing.

about 4 years ago · Juan Pablo Isaza Relatório

0

class in JavaScript is a syntactic sugar of constructor functions. When you work with functions, you are dealing with prototypes and prototypal inheritance, whereas in a class it's more of coupling these actions together within a blueprint, which again makes use of functions and prototypes under the hood. In the first case, the function greet is a prototype property of the function Dog, meaning the instance of Dog will be able to access the method greet, and the same applies to the second case as well. (Make sure to use the new keyword when instantiating objects in both cases).

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