Problem:
I am trying to call a class method from the onmouseover
event. Apparently I have no idea what I am doing.
Expected Result:
constructor says, this.cssId = app
method1 says, this.cssId = app
method2 says, this.cssId = app
Result:
constructor says, this.cssId = app
method1 says, this.cssId = app
method2 says, this.cssId = undefined
Code:
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<title>Test</title>
<style>
#app{
border: 5px solid orange;
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<div id='app'>Test</div>
<script>
class MyClass {
// Fields
cssId = 'app'
constructor() {
console.log('constructor says, this.cssId = ' + this.cssId);
}
// Methods
method2() {
console.log('method2 says, this.cssId = ' + this.cssId);
var appEl = document.getElementById(this.cssId);
}
method1() {
console.log('method1 says, this.cssId = ' + this.cssId);
var appEl = document.getElementById(this.cssId);
appEl.onmouseover = this.method2;
}
}
let thing = new MyClass();
thing.method1();
</script>
</body>
</html>
Thank you for your help.