问题
In his book "Secrets of the Javascript Ninja", John Resig writes:
Although dynamic code evaluation has been maligned due to its complexity and potential for security issues, without it we wouldn’t have had the CoffeeScript programming language.
I was hoping that someone could explain what is Dynamic Code Evaluation? Also, are there any code examples (or word examples) that would help better explain it? Furthermore, are there any website examples that use dynamic code evaluation?
Also, if possible, how did dynamic code evaluation bring about the CoffeeScript programming language?
回答1:
Dynamic code evaluation techniques in JavaScript:
- eval function
- Function object, created with the
Function
constructor
Basically you take a string (for example, concatenate it from parts) which contains JavaScript code, and use one of these techniques to parse and run it. The CoffeeScript compiler is actually a transpiler: it takes source code written in CoffeeScript (that is, a string), and translates it into JavaScript source code (another string), which is run using eval
.
来源:https://stackoverflow.com/questions/19614457/what-is-dynamic-code-evaluation-as-mentioned-by-john-resig