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

235
Vistas
Using call() method vs setting prototype property for inheritance

I'm a somewhat beginner, I used to know the very basics but stopped programming after that.

In JavaScript's Prototypal Inheritance, I've found/learned two ways of doing this.

Using the prototype property of a constructor:

function Mammal(name) {
    this.name = name,
    this.order = "Mammal",

    this.sayHi = function() {
        console.log("My name is " + this.name);
    }
}

function Cat(name) {
    this.name = name;
    this.family = "Felidae";
}

Cat.prototype = new Mammal();

let lion = new Cat("Lion");

Or by calling the Mammal.call(this, name) method.

What are the differences between the two results? Which one is to be preferred if they're different?

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

0

Usually, we use class keyword to define a class in many program languages. In Javascript you can define your classes using simple and clear syntax using class keyword.

Either you can use the two ways you mentioned, you can use class keyword:

class Mammal {
  constructor(name) {
    this.name = name;
    this.order = 'Mammal';
  }

  sayHi() {
    console.log('My name is ' + this.name);
  }
}

class Cat extends Mammal {
  constructor(props) {
    super(props);
    this.family = 'Felidae';
  }
}

let lion = new Cat('Lion');

lion.sayHi();

Read more about classes on MDN or W3S

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