When I try to call “Test” function I get an error.
How to fix that? (no jquery!)
Browser:firefox
error:
TypeError: this.Test is not a function
<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>
It’s because you reference
this.Fooas the event, what actually happens is that it copies that function out of the object scope, ergothisdoes not exist. What most people do is use an anonymous function / wrapper around the event.