Event as an object method

Hello,



var myObj = {

  event: document.onkeypress=function(e){
        var e=window.event || e;
        return e;
  }
  
  test1: function(param) {
  	//do something using this.event	
        if(this.event==...) {
        //...
        }
  }

 test2: function(param) {
  	//do something using this.event	
  }
  
  //...
}


As you can see, I’m trying to build an object which will have to use an event listener. I really don’t know if I’m going the right way with it amd am seeking some advice.

The object would be used like that:


if(myObj.test1(param)===true){
	//do something
}
if(myObj.test2(param)===true){
	//do something
}

How should this situation be handled? Should I use an event binder? If yes, how?

:slight_smile:

Can you go into a little more detail about what you’re trying to do? I’m not sure what you’re asking… though I’m almost positive that your code doesn’t do what you want.

Your code will set myObj.event to a function, because it sees the following JS…


event: document.onkeypress = function () { /* ... */ }

…and thinks you mean, “set document.onkeypress to a function, and then set myObj.event to whatever document.onkeypress is set to.”

Thanks for your reply :slight_smile:

I would like to the event object to be returned.

:slight_smile: