Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

75
Vistas
Object.Create calling function with prototype

Oke, I have this working:

const personProto2 = {
  calAge() {
    console.log(2021 - this.birthday);
  },
};
const rein = Object.create(personProto2);
rein.name = "Rein";
rein.birthday = 1945;
rein.calAge();

But if I do this:

const Person = function (name, birthday) {
  this.name = name;
  this.birthday = birthday;
};

Person.prototype.calAge = function () {
  console.log(2021 - this.birthday);
};
const rein = Object.create(Person);
rein.name = "Rein";
rein.birthday = 1945;
rein.prototype.calAge();

It doesn't work. But a function is also a object. And a object has also a prototype.

So why the second example doesn't work?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I think that you meant using new when creating a blank, plain JavaScript object.

The new operator lets you create an instance of a user-defined object type or of one of the built-in object types that has a constructor function. now, you can call the calAge method.

function Person(name, birthday) {
  this.name = name;
  this.birthday = birthday;
};

Person.prototype.calAge = function () {
  console.log(2021 - this.birthday);
};

const rein = new Person("Rein", 1945);
rein.calAge();

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda