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

228
Visualizações
How to call a child static function from parent in javascript?

Maybe what i'm looking for is impossible but it would be very convenient for my needs

class Music extends Database {
    static ref(id){return `musics/${id}`}
}

class Book extends Database {
    static ref(id){return `books/${id}`}
}

class Database {
    static fetch(id){
        const data = request(Database.children.ref(id)) // <===== HERE of course the 'children' function doesn't exists 
        return new this(data)
    }
}

Music.fetch(‘1234’) 

Book.fetch(‘9876’)

How to call a the static ref function from the static fetch function in the parent ?

This will avoid to copy the 'fetch' function in all children class

If this is an anti-pattern solution, what can I do ?

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

0

The relationship is the opposite: children can refer to their parent.

You can have a generic static method in the parent (Database) that the children just call with appropriate data:

class Database {
    static fetch(data, type){
        console.log(`requesting data: ${data} for type: ${type}`);
    }
}

class Music extends Database {
    static fetch(id){ return super.fetch(`musics/${id}`, "Music"); }
}

class Book extends Database {
    static fetch(id){ return super.fetch(`musics/${id}`, "Book"); }
}


Music.fetch('1234')
Book.fetch('9876')

Alternatively, the parent can still access data from this, so you can use it to access the child:

class Database {
    static fetch(data){
        console.log(`requesting data: ${data} for type: ${this.myType}`);
    }
}

class Music extends Database {
    static myType = "Music";
    static fetch(id){ return super.fetch(`musics/${id}`); }
}

class Book extends Database {
    static myType = "Book";
    static fetch(id){ return super.fetch(`musics/${id}`); }
}


Music.fetch('1234')
Book.fetch('9876')

Your code could be done with using this

class Database {
    static fetch(id){
        console.log(`requesting data: ${this.ref(id)}`);
    }
}

class Music extends Database {
    static ref(id){return `musics/${id}`}
}

class Book extends Database {
    static ref(id){return `books/${id}`}
}


Music.fetch('1234')
Book.fetch('9876')

However, I would advise against it. It puts all the responsibility of the inheriting classes to work the same, instead of putting all the logic in the parent and the inheriting classes do the minimal work necessary.

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