Section 24.79. eval( ): execute JavaScript code from a string


24.79. eval( ): execute JavaScript code from a string

ECMAScript v1

24.79.1. Synopsis

eval(code)

24.79.1.1. Arguments

code

A string that contains the JavaScript expression to be evaluated or the statements to be executed.

24.79.1.2. Returns

The value of the evaluated code, if any.

24.79.1.3. Throws

SyntaxError

Indicates that code does not contain legal JavaScript.


EvalError

Indicates that eval( ) was called illegally, through an identifier other than "eval", for example. See the restrictions on this function described in the next section.


Other exception

If the JavaScript code passed to eval( ) generates an exception, eval( ) passes that exception on to the caller.

24.79.2. Description

eval( ) is a global method that evaluates a string of JavaScript code in the current lexical scope. If code contains an expression, eval evaluates the expression and returns its value. If code contains a JavaScript statement or statements, eval( ) executes those statements and returns the value, if any, returned by the last statement. If code does not return any value, eval( ) returns undefined. Finally, if code throws an exception, eval( ) passes that exception on to the caller.

eval( ) provides a very powerful capability to the JavaScript language, but its use is infrequent in real-world programs. Obvious uses are to write programs that act as recursive JavaScript interpreters and to write programs that dynamically generate and evaluate JavaScript code.

Most JavaScript functions and methods that expect string arguments accept arguments of other types as well and simply convert those argument values to strings before proceeding. eval( ) does not behave like this. If the code argument is not a primitive string, it is simply returned unchanged. Be careful, therefore, that you do not inadvertently pass a String object to eval( ) when you intended to pass a primitive string value.

For purposes of implementation efficiency, the ECMAScript v3 standard places an unusual restriction on the use of eval( ). An ECMAScript implementation is allowed to throw an EvalError exception if you attempt to overwrite the eval property or if you assign the eval( ) method to another property and attempt to invoke it through that property.

24.79.3. Example

 eval("1+2");        // Returns 3 // This code uses client-side JavaScript methods to prompt the user to // enter an expression and to display the results of evaluating it. // See the client-side methods Window.alert() and Window.prompt( ) for details. try {     alert("Result: " +  eval(prompt("Enter an expression:",""))); } catch(exception) {     alert(exception); } var myeval = eval;  // May throw an EvalError myeval("1+2");      // May throw an EvalError 




JavaScript. The Definitive Guide
JavaScript: The Definitive Guide
ISBN: 0596101996
EAN: 2147483647
Year: 2004
Pages: 767

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net