面向对象的javascript - this.calcSomething不是一个函数

时间:2018-01-11 10:33:03

标签: javascript

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;
&#13;
&#13;

和simple.js

&#13;
&#13;
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;
&#13;
&#13;

加载页面,window.onload它进入calcSomething并向我显示警报,但当我单击按钮时,它进入buttonClick_Handler,但执行this.calcSomething();呼叫,控制台告诉我

this.calcSomething不是一个函数。 (在&#39; this.calcSomething()&#39;中,this.calcSomething&#39;未定义)

有人有任何想法吗?

0 个答案:

没有答案
相关问题