Javascript Eval and Function constructor usage
<p>I'm currently experimenting on self replicating code. Out of love for the language I'd like to write it in javascript.</p>

<p>I'm working on a program that writes a function's code which in turn writes its function own code and so on. Basically, the desired process is this:</p>

<ol>
<li>I manually create A function which returns code (which includes some randomness) and a numeric value (proposed solution to a problem). </li>
<li>I call this function a number of times, evaluate the results of each of those returned functions, and continue the process until I have code that is sufficiently good for what I'm trying to do.</li>
</ol>

<p>Now, I have always been told how <a href="http://blogs.msdn.com/b/ericlippert/archive/2003/11/01/53329.aspx" rel="nofollow" title="eval is evil">eval is evil</a>, how <a href="http://javascript.crockford.com/code.html" rel="nofollow">never to use it</a> and so on. However for my specific use case it seems like the Function constructor or eval are exactly what I'm looking for.</p>

<p><strong>So, in short the question is:</strong></p>

<p>Are eval/Function constructor indeed the best tools to use in my case? If so, I figured I'd use the Function constructor to scope the code executed, but is there a way to truly limit it from accessing the global scope? Also, what are some good practices for eval usage in my case? </p>
