When you click the text But no success when you click me, an error occurs. I know why the error occurs. My question is what is the best fix? The error occurs because when x.a is called when mydiv is clicked, this is mydiv. How can we make it so that x.a runs successfully when mydiv is clicked?
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div id="mydiv">But no success when you click me</div>
<script>
var x = new Thing();
var y = document.getElementById('mydiv');
x.a();
y.addEventListener('click', x.a, false);
function Thing() {
this.a=function() {
this.b();
}
this.b=function() {
alert('Success');
}
}
</script>
</body>
</html>