So I'm trying to keep my javascript project organised by using classes and an init function, now I'm having this weird problem I never have experienced before. This is my code:
class App {
constructor() {
this._username;
}
authenticate() {
this._username = "test";
}
submitForm() {
console.log(this._username);
}
}
const init = function () {
const app = new App();
document.getElementById("authenticationVerify").onclick = app.authenticate;
document.getElementById("btnSendSMS").onclick = app.submitForm;
}
window.onload = init;
And HTML (simplified)
<body>
<main>
<form id="formSendSMS">
<input type="button" name="send" id="btnSendSMS" value="Versturen">
</form>
<div id="authentication-prompt">
<form>
<input type="button" name="login" id="authenticationVerify" value="Log in" class="button blue">
</form>
</div>
</main>
<script src="js/mainBack.js" type="text/javascript"></script>
</body>
</html>
As you can see in the console, I get "undefined" when I activate the second button press, but I really don't know why this is happening, since the 'app' is not getting redeclared.
Kind regards, Jasper