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

125
Visualizações
Inheriting functions from 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
}

Given an object like this, I would like to inherit from it and give it a new property. For example, anotherGame extends startGame and takes all other properties, but I want to write additional code in addition to the properties of start function of startGame except for anotherStart function. What should I do ?

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

0

Under the hood, ES6 classes use prototype to achieve inheritance:

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();

Using ES6 classes:

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 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