Would it be better practice for event handlers that is an object member for have this
to refer to the object that is calling the event handler or it to refer to the object that the event handler is a member of?
For the first case, this would be an example of what it would look like:
var objects = [
/* ... */
{
a: 5,
b: 8,
/* other member variables */
onSomeEvent: function(data) {
/* Do stuff with data and this.
The a and b member variables are referenced with this.effect with the library accessed through this */
}
},
/* ... */
];
function someLibrary() {
this.doSomeEvent = function(handler, data) {
this.effect = handler;
handler.onSomeEvent.call(this, data);
}
}
var someLibraryInstance = new someLibrary();
someLibraryInstance.doSomeEvent(objects[1], {c:83,d:123});
While for the second case, objects[1].onSomeEvent
would look like this:
onSomeEvent: function(library, data) {
/* Do stuff with library, data and this.
The a and b member variables are accessed with this.a and this.b. The library is accessed through library */
}
while someLibrary.doSomeEvent
would look like this:
this.doSomeEvent = function(handler, data) {
handler.onSomeEvent(this, data);
}