title: Recommended way to Execute a function stored as String in JavaScript instead of using eval
body: <p>Before anyone marks it as duplicate, <a href="https://stackoverflow.com/questions/9683987/how-to-execute-function-stored-as-a-string-without-eval">this post</a> does not actually answer the question but suggests a different way altogether to solve that particular issue.</p>
  
  <p>Mine is a different issue. Please let me explain.</p>
  
  <p>In my case, there are various <code>.js</code> files (plugins) which are being loaded with <code>jquery getscript</code> and stored in variables. <em>Then whenever required they will be executed (more than once)</em></p>
  
  <p><strong>The code for loading script</strong> (this code will only run once at the init of the system for each plugin js file)</p>
  
  <pre><code>var storedFunc;
  $.getScript(pathToPluginJSFile, function( data, textStatus, jqxhr ) {
      storedFunc = data;    
  });
  </code></pre>
  
  <p><strong>All the plugins are in this format</strong></p>
  
  <pre><code>(function(){
    //lots of code here
  })()
  </code></pre>
  
  <p>But when I checked the <code>storedFunc</code> variable in console, I found out that it has been stored as String variable. Like this,</p>
  
  <pre><code>"(function(){
        //lots of code here
   })()"
  </code></pre>
  
  <p>Now to execute this, I used <code>eval</code>, like this (this code can be executed multiple times based on the need)</p>
  
  <pre><code>eval(storedFunc)
  </code></pre>
  
  <p>Everything is working fine and i am happy with it, but <strong>here comes the problem</strong>, I read in somewhere that the usage of <code>eval</code> is kind of like a bad thing to do. So now I am afraid that thought everything is working fine, all these negativity of using eval spread on the internet might scare my client away. :(</p>
  
  <p>So, please tell me how I can run that stored function (which has become a string) without using eval.</p>
  
  <p>Or should I use anything else than <code>$.getScript</code> which does not convert a function into a string ?</p>
  
  <p>Or if there is any other way altogether rewriting this plugin functionality?</p>
  
  <p>Please show me the way. I am in need of this solution badly.</p>
  
  <p>Any help will be appreciated.</p>
  
  <p>Thanks in advance.</p>