1

I have a prototype function such as event.

Prototype

Func("input_element_id").event("keyup",function(){
    alert("Works on keyup in an Input!");
}

Func.prototype= {
    keyup: function(func){
         //adding event listener and callback using func
    },
    event: function(e,func) {
         //what to do here to call function "keyup"
    }
};

The prototype name is in the variable e. But how can i call that function using the variable name?

I am doing this, so that passing "keyup keydown" will add keyup and keydown listeners, which is better than calling each prototype function individually.

Thanks.

3 Answers 3

2
var Func = function(element){
  this.element = element;
};

Func.prototype = {
    keyup: function(handler){
      this.element.onkeyup = handler;
    },
    event: function(e,handler){
        switch(e){
            case 'keyup':
              return this.keyup(handler);
            break;
        }
    }
}

http://jsfiddle.net/YDekw/

Sign up to request clarification or add additional context in comments.

Comments

0

Access the receiving object with this.

this.keyup(func);

Assuming a decent browser or an ES5 shim, looks like you want to do this:

e.split(" ").forEach(function(f) {
    this[f](func);
}, this);

Note that it’s just whatever object is being used with the dot operator, though, and could be anything. For example:

var f = new Func("input_element_id");
window.event = f.event;
window.event(); // this is window

12 Comments

e is a string representing the property OP wants to access. You should be doing this[e](func)
.event("keyup",function(){ alert("Works on keyup in an Input!"); }
@Harikrishnan: That won’t work space-separated at all. See update. I was debating whether to add that, but looks like I may as well.
@minitechi i figured how to do when coming into spaces. But dont know how to access when that is in a variable or when e is a string representing the property OP wants to access
Your answer was this.keyup(func);. You simply failed to read the question. Using this was certainly part of the issue though. Either way, your .forEach() solution is incorrect.
|
0

The best way to achieve this is by:

events: function(events,func) {
            this[events](func);
        }
    }

Thanks to CrazyTrain, As he mentioned in Comments.

3 Comments

So what happened to the spaces?
@minitech i didnt ask question with adding spaces. I just mentioned why i am doing like this.
To quote your question, I am doing this, so that passing "keyup keydown" will add keyup and keydown listeners, which is better than calling each prototype function individually.. That makes sense… if you just want to add an event by name, instead of making a different function for each one, I’d use element.addEventListener(singleEventName, func);. (With appropriate IE compatibility if you’d like.) Making a different function for each one is probably going to be a bit tedious.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.