tags:

views:

44

answers:

2
+4  A: 

You can't call killSwitch because you defined the method as a property of the object instance (this.killSwitch).

You can't use this inside the keypress event, because it will refer to the document, you have to store the this value:

var ClassA = function() {  
    var doc = document, 
              instance = this; // store reference to `this`

    doc.onkeypress = function(e){ instance.killSwitch(); }; 
    this.killSwitch = function(){ alert('hello world'); };
}

var myClass = new ClassA();
CMS
Rats, you beat me to it by 30 seconds! +1, nice answer
Josh
A: 

Try:

var ClassA = function()  
{  
    var doc = document;
    var killSwitch = function(){ alert('hello world'); };
    killSwitch();

    doc.onkeypress = function(e){ killSwitch(); }  
    this.killSwitch = killSwitch  
}

var myClass = new ClassA();

This way you define the killSwitch function inside the ClassA function, creating a closure, and it is available both within and outside the class.

Josh