I have this piece of code:
class TestComponent {
GenerateView() {
var btntest = document.getElementById('btntest');
btntest.onclick = function() {
// this.test(); //this doesn't work
alert("ok"); //this works
}
}
test() {
alert("ok");
}
}
var d = new TestComponent();
d.GenerateView();
<div id="mydiv">
<button id="btntest">test</button>
</div>
Why I can't use test
function
in onclick
event for btntest
? I have an error
this.test
is not a function
When I didn't wrap in function
, It worked.