I have the following module class and to make it work similar to non module functional javascript, I have to call a function to bind all the class member functions to the correct this scope and I have to explicitly add this. when referencing class members.
Is there a way to remove these requirements so that module code looks exactly the same as functional code (see how it works in typescript in nodejs).
The only thing to pay attention to in the code examples is the addition of this. and the requirement to call bindProperties() function in the module class. If you don't call the bindProperties() function the variables and the classes and the event handlers all lose scope. The this in those cases could be anything. The content of the example code is not important.
Including javascript in an HTML page (notice no mention of this in any of the code or in the event handler):
<script src="myscript.js"></script>
// myscript.js
"use strict"
var button = document.getElementById("button");
button.addEventListener("click", buttonClicked);
function buttonClicked(event) {
alert("Hello world");
}
The same thing in a module class is the following (notice the multiple uses of this. and the necessary call to bind() in the constructor):
<script src="myscript.js" type="module"></script>
// myscript.js
export App class {
button = null;
constructor() {
try {
this.bindProperties(App);
this.button = document.getElementById("button");
this.button.addEventListener("click", this.buttonClicked);
}
catch(error) {
this.log(error);
}
}
buttonClicked(event) {
alert("Hello world");
}
// this method is required to make the `this.` point to the class in all of the functions and event handlers
bindProperties(mainClass) {
var properties = Object.getOwnPropertyNames(mainClass.prototype);
for (var key in properties) {
var property = properties[key]
if (property!=="constructor") {
this[property] = this[property].bind(this);
}
}
}
}
What I'd like to know is if there is a setting to remove the need for needing to write this. and to remove the need to call, bindProperties(). Note the lack of this. and the lack of bindProperties():
<script src="myscript.js" type="module" version="2"></script>
// myscript.js
export App class {
button = null;
constructor() {
try {
button = document.getElementById("button");
button.addEventListener("click", buttonClicked);
}
catch(error) {
console.log(error);
}
}
buttonClicked(event) {
alert("Hello world");
}
}
Basically, is there an option to remove the need for adding this and an option to remove the need for calling bindProperties.
What am I talking about? Look at typescript/javascript in nodejs. You never need to use this., you don't need to call binding function on classes. The scope is always what you expect it to be.
But for client side javascript, I'm using typescript it is aware of the scope issue and by default it adds this. to any class members with code complete. And it flags it if it doesn't have it.
This is confusing for Javascript newbies who might not understand why they need to bind the this on class modules when they don't have to do it in non module scripts or in nodejs.
But maybe I'm missing something. I hope this makes sense.
Again, if you write code in a normal script included in a web page, you don't need to do any of these scope things that you have to do when using a module.
In other words, is there a setting to make modules code and behavior syntactically the same as non modules / nodejs code? The answer is probably no obviously but typescript has so many options maybe I missed it.
Update
In this linked post is this comment:
Changing myVariable to this.myVariable fixes the issue, but doing that for every variable clutters up my code quite a bit.
And once he starts building modules he will have the other issues I mention above.
There are actually just two technical approaches, which both lead to the desired behavior.
First, an anonymous arrow function expression as handler makes use of retaining the
thisvalue of the enclosing lexical context, hence theAppinstantiation time'sthis.Second, a single function statement for handling any
Appinstance's button-click. It is the base for creating handler-functions which explicitly dobindanAppinstance as the created handler'sthiscontext.