JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript eval有关的电子书:

4.12 Evaluation Expressions

data[i++] *= 2; data[i++] = data[i++] * 2;

4.12 Evaluation Expressions

Like many interpreted languages, JavaScript has the ability to interpret strings of Java-Script source code, evaluating them to produce a value. JavaScript does this with the global function eval():

eval("3+2") // => 5

Dynamic evaluation of strings of source code is a powerful language feature that is almost never necessary in practice. If you find yourself using eval(), you should think carefully about whether you really need to use it.

The subsections below explain the basic use of eval() and then explain two restricted versions of it that have less impact on the optimizer.

Is eval() a Function or an Operator?

eval() is a function, but it is included in this chapter on expressions because it really should have been an operator. The earliest versions of the language defined an eval() function, and ever since then language designers and interpreter writers have been placing restrictions on it that make it more and more operator-like. Modern JavaScript interpreters perform a lot of code analysis and optimization. The problem with eval() is that the code it evaluates is, in general, unanalyzable. Generally speaking, if a function calls eval(), the interpreter cannot optimize that function. The problem with defining eval() as a function is that it can be given other names:

var f = eval; var g = f;

4.12 Evaluation Expressions | 79

If this is allowed, then the interpreter can’t safely optimize any function that calls g(). This issue could have been avoided if evalwas an operator (and a reserved word). We’ll learn below (in §4.12.2 and §4.12.3 ) about restrictions placed on eval() to make it more operator-like.

友情链接It题库(| 版权归yishouce.com所有| 友链等可联系|粤ICP备16001685号-1