传递对象方法作为Javascript中的引用

时间:2012-05-03 04:39:33

标签: javascript function reference

当我按下空格键时,chrome显示错误:“未捕获TypeError:对象#没有方法'空格键'”

(firefox确实说“this.Spacebar不是函数”);

这是对象,由“Init();”(在页面加载...上)初始化:

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;
    }
}

初始化功能:

function Init() {
ChatManagement = new ChatManager();
LoginManagement= new Login();
KeyManagement= new KeyManager();
KeyManagement.switchKeySet("chat");
}

聊天管理对象:

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="";
}

}

“ChatManager的this.submitMessage()”工作

当我使用“console.log(this.Spacebar);”“switchKeySet()的末尾;”我得到的代码“这个.submitMessage()“即可。

当我在“this.KeyPress()”的开头使用它时,生病了“未定义”;

我试图避免多个switch语句和javascript-libaries,它们具有这种情况的功能......

有人知道错误在哪里吗? 我有这种感觉,“this.spacebar”获取未定义的“this.submitMessage”,但init首先初始化ChatManager,并在init完成后按空格键...

(或者不能像我试图的那样传递函数吗?)%_%。

1 个答案:

答案 0 :(得分:1)

这里的问题是当你进入this.KeyPress的函数时,'this'关键字具有不同的含义:

this.Spacebar = ChatManagement.submitMessage; // 'this' refers to the KeyManager function 
this.KeyPress = function(e) { 
        if(e.keyCode==13) { 
            this.Spacebar(); // 'this' refers to the caller of the function (keypress event)
        } 
    } 
 ....
    document.onkeypress=this.KeyPress;  
  }

查看How does the "this" keyword work?的答案以获得更清晰的图片,但看起来您需要将代码更改为:

function KeyManager() {
var self = this;      
self.mode="none";     

self.Spacebar = ChatManagement.submitMessage;     
self.KeyPress = function(e) {     
        if(e.keyCode==13) {     
            self.Spacebar();     
        }     
    }     
self.switchKeySet= function(value) {     
    if(self.mode!=value) {     
        self.mode=value;     
        switch(value) {     
            case "login":     
            self.Spacebar = LoginManagement.SendLogin;     
            break;     
            case "chat":     
            self.Spacebar = ChatManagement.submitMessage;     
            break;                   
            default:     
            case "none":     
            break;     
        }     
    document.onkeypress=self.KeyPress;     
    }     
}