Run into a JavaScript issue that I can't understand. I'm making a JavaScript "class" that is supposed to draw some things on a canvas, and if the window gets resized, it's supposed to know how to resize the canvas on its own and fix orientation for the drawn objects.
Everything seems to work fine when I use this class from my own test html, but when I extracted my class into a separate .js file to be added as <script src="myclass.js"></script>
for other people's usage, it starts throwing a "TypeError: this.func02 is not a function"
error upon resize. This is annoying because this.func02 clearly is a function.
Also, there's a ticker constantly calling func03, and in the console I can clearly see logs being printed out periodically (those logs happen from func01, which is called by func03, in the exact same way that func02 calls it). Those logs keep working even after a TypeError showing up whenever I resize.
The JS:
function myClass() {
this.func01 = function() {
console.log("func1");
//do stuff
}
this.func02 = function() { //<- this doesn't work ???
this.func01();
//and do more stuff
}
this.func03 = function() { //<- this works ???
this.func01();
//stuff
}
window.addEventListener("resize", this.func02, false);
}
function useMe() {
objectOfMyClass = new myClass();
setInterval(tickerThatCallsFunc03ofMyClass, 50);
}
If anybody has any idea why this happens, please write stuff. I'm stumped on debugging this. I tried to remove all irrelevant code from my program, if you need more information post and I'll add it.
Note: the problem here isn't the keyword "this". Maybe it's part of the problem, but it's not all of it. The exact code works when it's in one file with the html, but doesn't work when it's added to the html with