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

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

Post a Comment

Popular posts from this blog

AngularJs: User friendly date display with AngularJs and MomentJs

Getting started with Grails functional tests using Geb + Spock

Nerd Tree: A File Explorer with Mac Vim