This is because in event listeners (and commonly in most callbacks) the function's "this", changes. In event listeners it's usually the element that triggers the event, in your case, this is actually your "el". To work around this you can do:
var FL = {
connectEvent : function(params) {
var params = params || {};
var listener = function () {
params.fn();
};
if (params.el.addEventListener) {
params.el.addEventListener(params.event, listener, false);
} else if (params.el.attachEvent) {
params.el.attachEvent('on' + params.event, listener);
} else {
params.el['on' + params.event] = listener;
}
}
};
FL.connectEvent({
event: 'resize',
el: window,
fn: function() {
console.log('start on resize');
console.log('wat is this.el? ', this.el);
console.log('end on resize');
}
});
That way the listener function inherits the context it's created in, so it has access to params and calling params.fn() would preserve it's "this". If you want to also get the arguments of the event callback you can do:
var listener = function () {
params.fn.apply(params, arguments);
};
The last one is equivalent to using:
var listener = params.fn.bind(params);
, but also works in older browsers.