How do I get a function from string in javascript?
<p>Consider the following code :</p>

<pre><code>function foo()
{
    alert(1);
}

var str = foo.toString();

var fn = Function(str); 

console.log(fn2.toString());
</code></pre>

<p>The output is :</p>

<pre><code>function anonymous() {
 function foo()
 {
     alert(1);
 }
} 
</code></pre>

<p>I am trying to get back the foo function and store it in fn .
But here the foo function is inside an Anonymous function.
I tried eval() . But it creates the foo function in global scope.</p>

<p>How do I get back the function from its stringified form ?</p>

<p><strong>UPDATE:</strong>
I am creating a map of functions to arguments.
To uniquely identify the functions, I used toString() method.</p>

<pre><code>var map[foo.toString()] = [1,2,3]; //this list is populated dynamically in my code

// Now I want to call foo with [1,2,3] as arguments. For this I need to get back foo() function
foo(1,2,3)
</code></pre>
