+ 2
Thats one of the many javascript quirks. btn.onclick = obj.getv will override the "this" variable inside the getv function. Event handlers specifically will make "this" into an event. (You can try `console.log(this.target)` inside your getv function to see it. You never defined a "target" but since "this" is something else, it's there) to fix it, you can do: btn.onclick = function(){ obj.getv(); } Or btn.onclick = () => obj.getv(); Or the more exotic, fixing the value of "this" so it can't be changed later: btn.onclick = obj.getv.bind(obj);
12th Sep 2017, 2:57 PM
Schindlabua
Schindlabua - avatar