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

241
Visualizações
How to overwrite a method with a new one which uses the old?

For example, I wanted to create a custom version of CanvasRenderingContext2D.lineTo(). The first thing I did was:

const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');

ctx.myLineTo = function(x, y) {
  this.lineTo(x + 200, y + 200);
}

ctx.strokeStyle = 'black';
ctx.beginPath();
ctx.moveTo(50, 50);
ctx.lineTo(50, 50);
ctx.stroke();
ctx.closePath();

and it worked. But I would prefer, if possible, to replace the lineTo method instead of adding myLineTo. The problem is that I need the first to create the second.

Unfortunately, this:

ctx.lineTo = function(x, y) {
  this.lineTo(x + 50, y + 50);
}

raises InternalError: too much recursion. Is there a way to copy the lineTo method and prevent this infinite recursion?

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

0

The answer is yes, here is how:

ctxLineTo = ctx.lineTo;
ctx.lineTo = function(x, y) {
  ctxLineTo.call(this, x + 50, y + 50);
}

(Be careful not to use an arrow function or else this will refer to the Window object.)

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