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

123
Vistas
Heredar funciones de JavaScript
const startGame = { time : 0, start : function(){ } next : function(){ } end : function(){ } } const anotherGame = { //want to inherit from startGame startAnother : function(){} //want to inherit from startGame.start }

Dado un objeto como este, me gustaría heredar de él y darle una nueva propiedad. Por ejemplo, anotherGame extiende startGame y toma todas las demás propiedades, pero quiero escribir código adicional además de las propiedades de la función de start de startGame excepto para la función anotherStart . Qué tengo que hacer ?

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

0

Debajo del capó, las clases de ES6 usan prototipos para lograr la herencia:

 const startGame = { time: 0, start: function () { console.log("works", this.time); }, next: function () {}, end: function () {}, }; const anotherGame = { //want to inherit from startGame startAnother: function () { this.start(); }, //want to inherit from startGame.start }; // Magic happens here Object.setPrototypeOf(anotherGame, startGame); anotherGame.startAnother();

Usando clases de ES6:

 class StartGame { time = 0; start() { console.log("works", this.time); } next() {} end() {} } class AnotherGame extends StartGame { startAnother() { this.start(); } } const anotherGame = new AnotherGame(); anotherGame.startAnother();

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