simple.html
<!doctype html>
<html>
<head>
<script src="simple.js"></script>
</head>
<body>
<form>
<input type="button" id="click_on_me" value="Click me">
</form>
</body>
</html>
&#13;
和simple.js
class SimpleClass
{
constructor()
{
this.buttoncalcElement = document.getElementById("click_on_me");
this.buttoncalcElement.onclick = this.buttonClick_Handler;
}
buttonClick_Handler()
{
alert("hi babes");
this.calcSomething();
}
calcSomething()
{
alert("bye babes");
}
}
window.onload = function()
{
simpleObject = new SimpleClass();
simpleObject.calcSomething();
}
&#13;
加载页面,window.onload它进入calcSomething并向我显示警报,但当我单击按钮时,它进入buttonClick_Handler,但执行this.calcSomething();呼叫,控制台告诉我
this.calcSomething不是一个函数。 (在&#39; this.calcSomething()&#39;中,this.calcSomething&#39;未定义)
有人有任何想法吗?