当我试图调用"Test“函数时,我会得到一个错误。
怎么解决这个问题?(没有jquery!)
浏览器:firefox
错误:
TypeError: this.Test不是一个函数
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">
MyClass = function(){
}
MyClass.prototype = {
Init: function(){
var txt = document.getElementById("text");
if (txt.addEventListener) {
txt.addEventListener("keyup", this.Foo, true)
}
},
Foo: function(){
this.Test();
},
Test: function(){
alert('OK');
}
}
window.onload = function(){
obj = new MyClass;
obj.Init();
}
</script>
</head>
<body>
<textarea id="text" rows="10">
</textarea>
</div>
</body>发布于 2010-06-05 14:02:55
这是因为您将this.Foo引用为事件,实际发生的情况是它将该函数复制到对象范围之外,因此不存在this。大多数人所做的就是对事件使用匿名函数/包装器。
https://stackoverflow.com/questions/2980567
复制相似问题