SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2011
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    event as an object method

    Hello,

    Code JavaScript:
     
    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:
    Code:
    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?


  2. #2
    SitePoint Addict sdleihssirhc's Avatar
    Join Date
    Feb 2009
    Posts
    387
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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...

    Code JavaScript:
    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."
    I'm the web overlord for Graphic Business Systems

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2011
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply

    I would like to the event object to be returned.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •