Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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){
        return request(Database.children.ref(id)) // <===== HERE of course the 'children' function doesn't exists 
    }
}

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading