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

120
Visualizações
How to alias a prototype function using es6 syntax

The requirement is to have an alias to a prototype function.

Currently I'm doing this, which is adding an additional function and execution context, when called, as opposed to just a reference:

class X {
  x() {
    // stuff
  }

  y() {
    this.x();
  }
}

Because I don't know how to do this, in es5:

function X() {

}
X.prototype.x = function () {
  // stuff
}
X.prototype.y = X.prototype.x;

Is it possible?

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

0

There are multiple ways to do this. The easiest is doing just the same as without class syntax:

X.prototype.y = X.prototype.x;

If you insist on class syntax, ES2022 will have static blocks:

class X {
    …
    static {
        // notice unlike a method definition, this creates an enumerable property
        this.prototype.y = this.prototype.x;
    }
}

If you need to limit yourself to ES2015, you can still provide the x method as y in various ways:

class X {
    …
    constructor() {
        // notice this creates an own, enumerable property
        this.y = this.x;
    }
}
class X {
    …
    // notice this prevents assignments to `.y`
    get y() {
        return this.x;
    }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You can do the same thing with classes as well, because they use the same underlying prototype-based structure.

So,

class X {
  x() {
    // stuff
  }
}

X.prototype.y = X.prototype.x

works fine.

If you want to define it in the class, the only way is your approach:

class X {
  x() {
    // stuff
  }

  y(...args) {
    return this.x(...args);
  }
}

However, this will still behave differently if x is overridden in a subclass (which might actually be what you want, if you want a true alias).

about 4 years ago · Juan Pablo Isaza Relatório

0

How about you add y in the constructor?

class X {
  constructor() {
    this.y = this.x;
  }
  x() {
    // stuff
  }
}

Of course this will add it as an instance property; this should be no issue though because all instances simply hold a reference to the prototype method in their y.

If that does not meet your requirements I'm kindly asking for a clarification.

Here's how you add it to the prototype:

class X {
  x() {
    console.dir(this.__proto__);
  }
}

X.prototype.y = X.prototype.x;

(new X).x();
(new X).y();

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