0

I am looking for the correct way to implement a javascript class without using a specific framework (react, angular and ...), jQuery dependency injection and access the instance object in the class methods (using the this keyword) when assigning callbacks to events.

The implementation I did is as follows:

Parent class:

import autoBind from "./auto-bind.js";

class Tools {
    constructor() {
        autoBind(this);
    }

    init() { }
}

export default Tools;

Sub class:

import autoBind from "./auto-bind.js";
import Tools from "./tools.js"
import * as hub from "./hub.js"

class PanelTools extends Tools {
    constructor() {
        super();

        autoBind(this);
    }

    async init() {
        let _this = this;

        super.init();

        await hub.start();

        $(document)
            .ajaxStart(function () {
                _this.showLoader();
            });
    }

    showLoader() {
        $("#loader").show();
    }
}
  • It should be noted that I am not using a special framework (react,
    angular and ...) here.

  • I used the "auto-bind" library to bind this.

  • I implemented the async init() method to call await functions.

  • I defined the variable _this in the class methods to access the
    instance.

Is it correct to implement these classes and use jquery in the methods?

Farzad
  • 93
  • 2
  • 10
  • If it works for you, then it's correct. Voting to close as opinion-based. – InSync Jun 15 '23 at 23:18
  • True, but it would be good if I could know the correct structure of this class and how to correctly inject the jQuery dependency. @InSync – Farzad Jun 15 '23 at 23:54

0 Answers0