Tuesday, April 20, 2010

Dynamic methods with javascript

I recently had an implementation requirement to be able to call a method given a string with the name of the method.

I came across two strategies for achieving this:

1. Eval


function a(){
alert('a called');
}
eval('a');


2. Map


function a(){
alert('a called');
}
window['a']();