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']();

2 comments:

  1. thanks! yes it did! saved me some time. Keep up the good work :)

    ReplyDelete
  2. JavaScript Training Course is essential to the achievements of an individual. Perhaps its most positive benefit is better employees. A company develop the potential of an employee, and part of the way a company encourages improvement is through training.

    JavaScript Course
    Online Javascript Course
    Javascript Course Online
    Free Online Javascript Course

    AngularJS Interview Questions
    JavaScript Tutorials

    ReplyDelete