3
votes

Calling private/public method from constructor in the same class gives me an error

Uncaught TypeError: this.setEventHandler is not a function at Object.Init (Init.ts:4) at :3:16

export class Init {

constructor(private connectBtn: HTMLElement) {
    this.setEventHandler();
}
public setEventHandler() {
    this.connectBtn.onclick = (e) => {
        this.openConnectWindow();
    }
}
private openConnectWindow() {
    console.log("OPEN SESAME")
}
}

This is the compiled javascript

var Init = (function () {
function Init(connectBtn) {
    this.connectBtn = connectBtn;
    this.setEventHandler();
}
Init.prototype.setEventHandler = function () {
    var _this = this;
    this.connectBtn.onclick = function (e) {
        _this.openConnectWindow();
    };
};
Init.prototype.openConnectWindow = function () {
    console.log("OPEN SESAME");
};
return Init;
}());

Edit: This is for a library.

I tried putting var mylib = new MyLibraryName() but I only get "MyLibraryName is not a constructor".

I start the root object with webpack

output: {
    path: path.join(__dirname, "lib"),
    filename: outputFile,
    library: libraryName,
    libraryTarget: "umd",
    umdNamedDefine: true
},
1
Show me how you create Init class instanses - degr
Its going to be a library so i just type MyLibraryName.Init - user56567675
var ele = document.getElementsByClassName('dropdown-toggle')[0]; MyLibraryName.Init(ele) - user56567675

1 Answers

2
votes

Change the methods you are calling from other methods to this

export class Init {

constructor(private connectBtn: HTMLElement) {
    this.setEventHandler();
}

public setEventHandler = () => {
    this.connectBtn.onclick = (e) => {
        this.openConnectWindow();
    }
}

private openConnectWindow = () => {
    console.log("OPEN SESAME")
}
}