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

254
Visualizações
How to "conventionally" (without class syntax) call a superconstructor in javascript that enforces the use of "new"?

I'd like to inherit some class MyClass from some built-in class Class in the conventional way, i.e. without the new "class" syntax. I'm used to the following:

MyClass.prototype = Object.create(Class.prototype);
function MyClass() {
   Class.apply(this, arguments); // calling the superconstructor => "TypeError: Class constructor: 'new' is required"
   ...
}
var myObject = new MyClass(...);

As indicated, for many newer classes, usage of "new" with the constructor is enforced (e.g. for MediaSource). But new will be used with MyClass and will already have created a new object, so that I should not create yet another object inside the constructor. What am I supposed to do, or what is the equivalent here of the super() call in the new class syntax?

The only way I could think of, is to indeed create an instance with new Class() and explicitly return this instance from the constructor. But then the prototype of MyClass (which might have additional properties) is not really used ...

about 4 years ago · Santiago Gelvez
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