0
var events = {
                addEvent: function(element, type, fn, capture){
                     element.addEventListener(type, fn, capture);
                },
                removeEvent: function(){
                     element.removeEventListener(type, fn, capture);
                }
           }

I'm loking for something like this in object literal definition to reduce the code the event Object is just an example

           var events = {
                            [action + "Event"]: function(element, type, fn, capture){
                                  element[action + "eventListener"](type, fn, capture);
                        }
                }
           }

I know there are other options like

element.events.add(....) , element.events.remove(....)

or this option

element.events("add", type, fn, capture) / element.events("remove", type, fn, capture)
Kamran Asgari
  • 2,107
  • 3
  • 21
  • 39

1 Answers1

1

Property names in object initializers must be constants. You can however add properties to an object and create names dynamically:

var events = {
            addEvent: function(element, type, fn, capture){
                 element.addEventListener(type, fn, capture);
            },
            removeEvent: function(){
                 element.removeEventListener(type, fn, capture);
            }
       };

events[action + "Event"] = function() { /* ... */ };
Pointy
  • 389,373
  • 58
  • 564
  • 602