Jump to content

Adding a function to a constructor from an external file

DriedSponge

How can I add a function onto this from an external file?
For example:

class SpongeHelpers{
    public html: any;
    constructor(sel) {
        if (!sel) return;
        if (typeof sel === 'function') {
            document.addEventListener('DOMContentLoaded', sel);
        }else if (typeof sel === 'string') {
            var selected = document.querySelectorAll(sel);
            console.log(this);
            this.html = (arg) => {
                selected.forEach((element) => {
                    element.innerHTML = arg;
                })
            };
        }

    }
    init(sel){
        return new SpongeHelpers(sel);
    }
}


As you can see, I define this.html within the class but I want to be able to define from an external file, so it's easier to add more functions in the future. I did some looking around and found out I need to do something with interfaces, however I do not know how to properly use them. Any help is much appreciated.

Remember to quote or @mention others, so they are notified of your reply

AMD Ryzen 9 5950X | Arctic Liquid Freezer II 360 | MSI B450 TOMAHAWK MAX | G.Skill Trident Z RGB 32 GB DDR4-3600 | Gigabyte GeForce RTX 2080 SUPER 8 GB 

Samsung 980 EVO Plus 2TB | SK hynix Gold S31 500GB SSD | Seagate Barracuda Compute 2 TB 7200RPM HDD | 1TB Samsung 860 EVO SSD | 3x Phanteks T30-120

Corsair RM1000e 1000 W 80+ Gold Certified Modular PSU | Corsair 5000D Airflow Windows 11 Home

Link to comment
Share on other sites

Link to post
Share on other sites

Remember to quote or @mention others, so they are notified of your reply

AMD Ryzen 9 5950X | Arctic Liquid Freezer II 360 | MSI B450 TOMAHAWK MAX | G.Skill Trident Z RGB 32 GB DDR4-3600 | Gigabyte GeForce RTX 2080 SUPER 8 GB 

Samsung 980 EVO Plus 2TB | SK hynix Gold S31 500GB SSD | Seagate Barracuda Compute 2 TB 7200RPM HDD | 1TB Samsung 860 EVO SSD | 3x Phanteks T30-120

Corsair RM1000e 1000 W 80+ Gold Certified Modular PSU | Corsair 5000D Airflow Windows 11 Home

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×