I've come across some code which allows you to convert a string of a function name to a function and use it:
var strFun = "someFunction";
var strParam = "this is the parameter";
//Create the function
var fn = window[strFun];
//Call the function
fn(strParam);
I was wondering if there was a way to do the same for object methods, e.g.:
var fn = window["onclick"];
var body = document.body;
body.onclick = function() {
alert('yo');
}
// This won't work [Uncaught TypeError: Object #<HTMLBodyElement> has no method 'fn']
body.fn(); // expecting body.onclick(); via substitution of fn with a onclick function
addEventListener.