when im pressing spacebar, chrome shows error: "Uncaught TypeError: Object # has no method 'Spacebar'"
(firefox does say "this.Spacebar is not a function");
here is the object, which goes to be initialised by "Init();"(on page load...):
function KeyManager() {
this.mode="none";
this.Spacebar = ChatManagement.submitMessage;
this.KeyPress = function(e) {
if(e.keyCode==13) {
this.Spacebar();
}
}
this.switchKeySet= function(value) {
if(this.mode!=value) {
this.mode=value;
switch(value) {
case "login":
this.Spacebar = LoginManagement.SendLogin;
break;
case "chat":
this.Spacebar = ChatManagement.submitMessage;
break;
default:
case "none":
break;
}
document.onkeypress=this.KeyPress;
}
}
Init function:
function Init() {
ChatManagement = new ChatManager();
LoginManagement= new Login();
KeyManagement= new KeyManager();
KeyManagement.switchKeySet("chat");
}
Chat Management Object:
function ChatManager() {
this.submitMessage = function() {
$("Message").focus();
var text = $("Message").value;
if(text==""){
this.write('<p class="warning">Please enter a message');
return;
}
try{
SendMessage(text);
this.write('<p class="event">Sent: '+text)
} catch(exception){
this.write('<p class="warning"> Error:' + exception);
}
$("Message").value="";
}
}
"this.submitMessage()" of ChatManager works
when i use "console.log(this.Spacebar);" at end of "switchKeySet();" i get the code of "this.submitMessage()".
when i use it at start of "this.KeyPress()", ill get "undefined";
im trying to avoid multiple switch statements and javascript-libaries which have a function for this case.....
does someone know where the error is? ive got the feeling, that "this.spacebar" gets the undefined "this.submitMessage" but init initializes ChatManager first and i press spacebar after init is done...
(or isnt it possible to pass functions around like i tried to?) %_%.